I made the following mini project for my own amusement which uses AWT and Database connectivity using ODBC. I can use either Oracle or MySQL for the back-end. Just make sure to make the correct connection.
Its a simple application which queries the database and inserts values. Got not much use but for practicing concepts in Java with a GUI application with database connectivity, it’ll work fine.
At any stage, feel free to ask question regarding the code as a comment. I’ll be happy to answer your queries.
Language : Java
[sourcecode language='java']
import java.awt.*;
import java.sql.*;
import java.awt.event.*;
public class MyApp extends Frame implements ActionListener, ItemListener
{
String msg=”";
Button b1,b2,b3;
TextField t1,t2,t4,t5;
Label l1,l2,l3,l4,l5;
Checkbox Male,Female;
CheckboxGroup grp;
String G;
public MyApp()
{
initComponents();
}
public void initComponents()
{
setTitle(“Employee Information”);
setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
b1=new Button(“Submit”);
b2=new Button(“Reset”);
b3=new Button(“NEXT”);
t1=new TextField(20);
t2=new TextField(20);
t4=new TextField(20);
t5=new TextField(20);
l1=new Label(“Emp ID : “);
l2=new Label(“Department : “);
l3=new Label(“Sex : “);
l4=new Label(“Name : “);
l5=new Label(“Salary : “);
grp=new CheckboxGroup();
Male=new Checkbox(“Male”, grp, false);
Female=new Checkbox(“Female”, grp,false);
Male.addItemListener(this);
Female.addItemListener(this);
b1.addActionListener(this);
b2.addActionListener(this);
b3.addActionListener(this);
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 0;add(l1,c);
c.gridx = 1;
c.gridy = 0;add(t1,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=1;add(l2,c);
c.gridx=1;
c.gridy=1;add(t2,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=2;add(l3,c);
c.anchor = GridBagConstraints.LINE_START;
c.gridx=1;
c.gridy=2;add(Male,c);
c.anchor = GridBagConstraints.LINE_END;
c.gridx=1;
c.gridy=2;add(Female,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=3;add(l4,c);
c.gridx=1;
c.gridy=3;add(t4,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=4;add(l5,c);
c.gridx=1;
c.gridy=4;add(t5,c);
c.anchor = GridBagConstraints.LAST_LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=1;
c.gridy=5;add(b1,c);
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(15,0,0,0);
c.gridx=1;
c.gridy=5;add(b2,c);
c.anchor = GridBagConstraints.LAST_LINE_END;
c.gridx=1;
c.gridy=5;add(b3,c);
setSize(300,300);
setVisible(true);
}
public void itemStateChanged(ItemEvent ie)
{
G=grp.getSelectedCheckbox().getLabel(); //Stores the Label of the saved Checkbox in String G
if(G.equals(“Male”))
G=”M”;
else
G=”F”;
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if(str.equals(“Submit”))
{
msg=”Please Confirm…”;
b1.setLabel(“Confirm”);
}
else if(str.equals(“Confirm”))
{
msg=”There was an Error!”;
}
else if(str.equals(“Reset”))
{
msg=”";
b1.setLabel(“Submit”);
t1.setText(“”);
t2.setText(“”);
t4.setText(“”);
t5.setText(“”);
Male.setState(false);
Female.setState(false);
repaint();
}
if(str.equals(“Confirm”))
{
try
{
Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection (“jdbc:odbc:Vaibhav”,”vaibhav”,”vaibhav”);
PreparedStatement ps=con.prepareStatement(“Insert Into emp Values(?,?,?,?,?)”);
ps.setInt(1,Integer.parseInt(t1.getText()));
ps.setString(2,t2.getText());
ps.setString(3,G);
ps.setString(4,t4.getText());
ps.setInt(5,Integer.parseInt(t5.getText()));
ps.executeUpdate();
con.close();
msg=”Successful!”;
}
catch(Exception sq)
{
System.out.println(sq);
}
}
if(str.equals(“NEXT”))
{
Second s=new Second();
setVisible(false);
s.setSize(300,300);
s.setVisible(true);
}
repaint();
}
public void paint(Graphics g)
{
g.drawString(msg, 5,285);
}
public static void main(String args[])
{
MyApp app=new MyApp();
}
}
class Second extends Frame implements ActionListener,ItemListener
{
String msg=”";
Button prev,srch;
TextField txtempid,txtname,txtdept,txtSex,txtSal;
Label lblname,lblempid,lbldept,lblSex,lblSal;
CheckboxGroup grp;
Checkbox chkName,chkEmp;
String srchBy;
public Second()
{
setTitle(“Search Database”);
prev=new Button(“PREV”);
srch=new Button(“Search”);
grp=new CheckboxGroup();
chkName=new Checkbox(“Name”,grp,false);
chkEmp=new Checkbox(“EMP”,grp,false);
lblempid=new Label(“EMP ID : “);
txtempid=new TextField(20);
lblname=new Label(“Name : “);
txtname=new TextField(20);
lbldept=new Label(“Dept : “);
txtdept=new TextField(20);
lblSex=new Label(“Sex : “);
txtSex=new TextField(1);
lblSal=new Label(“Salary : “);
txtSal=new TextField(20);
setLayout(new GridBagLayout());
GridBagConstraints c=new GridBagConstraints();
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 0;add(lblname,c);
c.gridx = 1;
c.gridy = 0;add(txtname,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=1;add(lblempid,c);
c.gridx=1;
c.gridy=1;add(txtempid,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=2;add(lbldept,c);
c.gridx=1;
c.gridy=2;add(txtdept,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=3;add(lblSex,c);
c.gridx=1;
c.gridy=3;add(txtSex,c);
c.anchor = GridBagConstraints.LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=4;add(lblSal,c);
c.gridx=1;
c.gridy=4;add(txtSal,c);
c.anchor = GridBagConstraints.LAST_LINE_START;
c.insets = new Insets(15,0,0,0);
c.gridx=0;
c.gridy=5;add(prev,c);
c.anchor = GridBagConstraints.LAST_LINE_START;
c.gridx=1;
c.gridy=5;add(srch,c);
c.anchor = GridBagConstraints.LINE_START;
c.gridx = 0;
c.gridy = 6; add(chkEmp,c);
c.anchor = GridBagConstraints.LINE_END;
c.gridx = 1;
c.gridy = 6;add(chkName,c);
prev.addActionListener(this);
srch.addActionListener(this);
chkName.addItemListener(this);
chkEmp.addItemListener(this);
}
public void itemStateChanged(ItemEvent ie)
{
srchBy=grp.getSelectedCheckbox().getLabel();
if(srchBy.equals(“Name”))
srchBy=”Name”;
else
srchBy=”EMP”;
}
public void actionPerformed(ActionEvent e)
{
String str=e.getActionCommand();
if(str.equals(“PREV”))
{
MyApp f=new MyApp();
setVisible(false);
f.setVisible(true);
}
if(str.equals(“Search”))
{
try{
Class.forName (“sun.jdbc.odbc.JdbcOdbcDriver”);
Connection con = DriverManager.getConnection (“jdbc:odbc:Vaibhav”,”vaibhav”,”vaibhav”);
if(srchBy.equals(“Name”))
{
PreparedStatement ps=con.prepareStatement(“Select * from emp where name=?”);
ps.setString(1,txtname.getText());
ResultSet rs=ps.executeQuery();
rs.next();
txtempid.setText(rs.getString(“Emp_ID”));
txtdept.setText(rs.getString(“Department”));
txtSal.setText(rs.getString(“Salary”));
txtSex.setText(rs.getString(“Sex”));
}
else if(srchBy.equals(“EMP”))
{
PreparedStatement ps=con.prepareStatement(“Select * from emp where EMP_ID=?”);
ps.setInt(1,Integer.parseInt(txtempid.getText()));
ResultSet rs=ps.executeQuery();
rs.next();
txtname.setText(rs.getString(“Name”));
txtdept.setText(rs.getString(“Department”));
txtSal.setText(rs.getString(“Salary”));
txtSex.setText(rs.getString(“Sex”));
}
else
msg=”Choose Search method!”;
con.close();
}
catch(Exception sq)
{
System.out.println(sq);
}
}
}
public void paint(Graphics g)
{
g.drawString(msg, 5,285);
}
}
[/sourcecode]







{ 2 comments… read them below or add one }
sir,
Please mail me this file
please mail me this file….