0% found this document useful (0 votes)
8 views3 pages

Java Database Connection and GUI Example

This document contains code for connecting to a database and performing basic CRUD operations in Java. It defines a database_connect class with methods to establish a connection, insert data into a table, and retrieve data from a table. The main method demonstrates inserting a record and retrieving all records from a table. A second code sample creates a simple GUI with a label, text field, button, and checkbox to write input text to a file when the button is clicked.

Uploaded by

Jamal Sharif
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views3 pages

Java Database Connection and GUI Example

This document contains code for connecting to a database and performing basic CRUD operations in Java. It defines a database_connect class with methods to establish a connection, insert data into a table, and retrieve data from a table. The main method demonstrates inserting a record and retrieving all records from a table. A second code sample creates a simple GUI with a label, text field, button, and checkbox to write input text to a file when the button is clicked.

Uploaded by

Jamal Sharif
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

import [Link].

*;

public class database_connect {


private ResultSet rs;
private Statement st;
private Connection con;

private void mycon(){


try {
[Link]("[Link]");
con = [Link]("jdbc:odbc:student");
}
catch(ClassNotFoundException e){
[Link]("Class Not Found::");
}
catch(SQLException e1){
[Link]("SQL Error1");
}
}

public void idata(String table, int rno, String name){


try {
mycon();
String query;
int result = 0;
query = "Inserted into" + table + "values(Roll Number, Student Name)" + rno + name;
st = [Link]();
result = [Link](query);
if (result>0)
[Link]("Value inserted");
else
[Link]("Values not inserted");
}
catch(SQLException e){
[Link]("Data not inserted..");
}
}

public ResultSet showall(String table){


try{
mycon();
st = [Link]();
rs = [Link]("select from " + table);
}
catch(SQLException e){
[Link]("SQL Error2");
}
return rs;
}

public static void main(String[] args){


ResultSet rs;
try {
database_connect db1 = new database_connect();
rs=[Link]("std");
[Link]("std",345,"bilal");
while ([Link]()){
[Link]([Link](1) +"\t");
[Link]([Link](2));
}
}
catch(SQLException e2){
[Link]("SQL Error3");
}
}
}

GUI
//[Link]

import [Link].*;
import [Link].*;
import [Link].*;
import [Link].*;
public class window {
static JFrame window = new JFrame();
static JButton b1;
static JCheckBox check;
static JLabel lb;
static Container content = [Link]();
static JTextField txt;

public static void main(String[] args) throws IOException {


// main windows
[Link]("Welcome to my Screen");
[Link](Color.LIGHT_GRAY);
Point center = [Link]().getCenterPoint();
[Link](center.x/4, center.y/4, 1024, 768);
[Link](JFrame.EXIT_ON_CLOSE);
[Link](true);
[Link](false);
FlowLayout layout = new FlowLayout([Link]);
[Link](layout);
//label
lb = new JLabel("Name:");
[Link](lb);
//text field
txt = new JTextField(20);
[Link](txt);

// add button
b1= new JButton("Ok");
[Link](b1);
[Link](100, 50, 200, 200);
// check box
check = new JCheckBox("BSCS",false);
[Link](check);
// action of button
[Link](new ActionListener() {

public void actionPerformed(ActionEvent e) {


try {
String text = [Link]();
File write = new File("[Link]");
PrintWriter writer = new PrintWriter(write);
[Link](text);
}
catch(FileNotFoundException e1){
[Link]();
}

[Link]([Link]);
}
});
//checkbox event
[Link](new ActionListener() {
public void actionPerformed(ActionEvent e){
[Link](false);
}
});

}
}

You might also like