Here I am showing simple application Swing- JDBC-Database



In this application, we make use of swing-jdbc-Database communication. For each database operation one button is given. For each button one separate listener class is written. In each listener class, event handler is implemented. Loading the driver, creating the connection and building the PreparedStatement object are done in the constructor of the window class.

/*

Source Code : EmployeeForm.java

*/
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.*;
class EmployeeForm extends JFrame
{

            JLabel l1,l2,l3,title,info;
            JTextField t1,t2,t3;
            JButton b1,b2,b3,b4,exit;
            Connection com;
            PreparedStatement insertps;
            PreparedStatement updateps;
            PreparedStatement deleteps;
PreparedStatement selects;
EmployeeForm()
{
            setSize(355,300);
            setLocation(100,100);
            Container c = getContentPane();
            Title = new JLable(“EMPLOYEE DETAILS”);
            Title.setFont(new Font(“Dialog”,Font.BOLD,15));
            l1=new JLabel(“EMPNO”);
            l2= new JLabel(“NAME”);
            l3= new JLabel(“SALARY”);
            t1= new JTextField(10);
            t2= new JTextField(10);
            t3= new JTextField(10);
            b1=new JButton(“INSERT”);
            b2=new JButton(“DELET”);
            b3=new JButton(“UPDATE”);
            b4=new JButton(“SHOW”);
            exit=new JButton(“EXIT”);
            c.setLayout(null);
            title.setBounds(60,10,160,20);
            c.add(title);
            l1.setBounds(40,40,50,20);
            c.add(l1);
            t1.setBounds(95,40,108,20);
            c.add(t1);
            l2.setBounds(40,70,50,20);
            c.add(l2);
            t2.setBounds(95,70,108,20);
            c.add(t2);
            l3.setBounds(40,100,50,20);
            c.add(l3);
            t3.setBounds(40,100,50,20);
            c.add(t3);
            b1.setBounds(10,140,65,40);
            c.add(b1);
            b2.setBounds(77,140,65,40);
            c.add(b2);
            b3.setBounds(144,140,65,40);
            c.add(b3);
            b4.setBounds(211,140,65,40);
            c.add(b4);
            exit.setBounds(278,140,65,40);
            c.add(exit);
            info= new JLabel(“Greetings connected to the database”);
            info.setFont(new Font(“Dialog”, Font.BLOD,15));
            info.setBounds(20,190,330,30);
            c.add(info);
            b1.addActionListener(new InsertListener());
            b2.addActionListener(new DeleteListener());
            b3.addActionListener(new UpdateListener());
            b4.addActionListener(new ShowListener());
            exit.addActionListener(new ExitListener());
            setVisible(true);
            getConnection();
}//Constructor
void getConnection()
{
            try
            {
                        Class.forName(“sun.jdbc.pdbc.jdbcOdbcDriver”);
                        String url=”jdbc:odbc:student”;
                        Con=DriverManager.getConnection(url,”scott”,”tiger”);
                        Info.setText(“Connection is established with the database”);
                        Insertps=con.prepareStatement(“insert into employee values(?,?,?)”);
                        Updateps=con.prepareStatement(“update employee set name=?,salary=? Where empno=?”);
                        Deleteps=con.pareapareStatement(“delete from employee where empno=?”);
                        Selects=con.prapareStatement(“select * from employee where empno=?”);
            }//try
            catch(ClassNotFoundException e)
            {
                        System.out.println(“Driver class not found……….”);
                        System.out.println(e);
            }
            catch(SQLException e)
            {
                        info.setText(“Unable to get connected to the database”);
            }
}//getConnection()

class InsertListener implements ActionListener
{
            public void actionPerformed(ActionEvent e)
            {
                        try
                        {
                                    int empno=Integer.partInt(t1.getText());
                                    String name=t2.getText();
                                    Float salary=Float.parseFloat(t3.getText());
                                    Insetps.setInt(1,empno);
                                    Insetps.setInt(2,ame);
                                    Insetps.setInt(3,mpno);
                                    Insetps.executeUpdate();
                                    Info.setText(“One row inserted successfully”);
                                    Insertps.clearParameters();
                                    T1.setText(“”);
                                    T2.setText(“”);
                                    T3.setText(“”);
                        }//try
                        catch(SQLException se)
                        {
                                    info.setText(“Failed to insert a record………”);
                        }
                        catch(Exception de)
                        {
                                    info.setText(“enter proper data before insertion…..”);
                        }
            }//actionPerfomed()
}//Listener class

class DeleteListener implements ActionListener
{
            public void actionPerformed(ActionEvent e)
            {
                        try{
                        int empno=Integer.parseInt(t1.getText());
                        deleteps.setInt(1,empno);
                        deleteps.executeUpdate();
                        deleteps.clearParameters();
                        info.setText(“One row deleted successfully”);
                        t1.setText(“”);              
                        t2.setText(“”);
                        t3.setText(“”);
                        }//try
                        catch(SQLException se)
                        {
                                    info.setText(“Failed to delete a record……..”);
                        }
                        catch(Exception de)
                        {
                                    info.setText(“Enter proper empno before deletion ……”);
                        }
            }//event handler
}//listener class
class UpdateListner implements ActionListener
{

            public void actionPerformed(ActionEvent e)
            {
                        try
                        {
                        int empno=Intergerr.parseInt(t1.getText());
                        String name=t2.getText();
                        Float slary=Float.parseFloat(t3.getText());
                        Updateps.setString(1,name);
                        Updateps.setString(2,salary);
                        Updateps.setString(3,empno);
                        Updateps.executeUpdate();
                        Info.setText(“One row updated successfully”);
                        Updateps.clearParameters();
                        T1.setText(“”);
                        T2.setText(“”);
                        T3.setTexgt(“”);
                        }//try
                        catch(SQLException se)
                        {
                                    System.out.println(se);
                                    Info.setText(“Failed to update the record…..”);
                        }
                        catch(Exception de)
                        {
                                    System.out.println(de);
                                    Info.setText(“Enter proper data before selecting updation…”);
                        }
            }//event handler
}
class ShowListner implements ActionListener
{
            try
            {
                        int empno=Integer.parseInt(t1.getText());
                        selectps.setInt(1,empno);
                        selectps.execute();
                        ResultSet rs = selectps.getResultSet();
                        Rs.next();
                        T2.setText(rs.getString(2));
                        T3.setText(“”+rs.getFloat(3));
                        Info.setText(“One row displayed su
Ccessfully”);
            }//try
            catch(SQLException se)
            {
                        info.setText(“Failed to show the record……………”);
            }
            catch(Exception de)
            {
                        info.setText(“enter proper empno before selecting show….”);
            }
   }//event handler
}//listener class

class ExitListener implements ActionListener
{
            public void actionPerformed (ActionEvent e)
            {
                        try{
                                    insertps.close();
                                    deleteps.close();
                                    updateps.close();
                                    selectps.close();
                                    if(cont!=null) con.close();
                                    System.exit(0);
                              }
                             catch(SQLExcetpion se)
                           {
                                    System.out.println(se);
                          }
            }
}
public static  void main(String args[])
{
            new EmployeeForm();
}
}//EmployeeForm class


Post a Comment

 
Top