Swing Form with Query Clear Next Previous Operations

  1. import javax.swing.*;  
  2. import javax.swing.table.DefaultTableModel;  
  3. import java.awt.*;  
  4. import java.awt.event.*;  
  5. import java.sql.*;  
  6. import java.util.Vector;  
  7. public class Registration extends JFrame implements ActionListener  
  8. {  
  9.     JLabel[] lbl_obj = new JLabel[20];  
  10.     JTextField[] tfld_obj = new JTextField[20];  
  11.     String[] fld_value = new String[20];  
  12.     JButton[] btn_obj = new JButton[8];  
  13.     JLabel l1;  
  14.     JLabel[] btn_name = new JLabel[8];  
  15.     //JPanel jp1, jp2, jp3;  
  16.     ResultSet rs;  
  17.     JTable table;  
  18.     Registration()  
  19.     {  
  20.         table = new JTable();  
  21.         l1 = new JLabel("Employee Details:");  
  22.         l1.setForeground(Color.blue);  
  23.         l1.setFont(new Font("Serif", Font.BOLD, 20));  
  24.         lbl_obj[0] = new JLabel("First Name:");  
  25.         lbl_obj[1] = new JLabel("Last Name:");  
  26.         lbl_obj[2] = new JLabel("Email");  
  27.         lbl_obj[3] = new JLabel("phone number");  
  28.         lbl_obj[4] = new JLabel("Hire Date :");  
  29.         lbl_obj[5] = new JLabel("Job ID:");  
  30.         lbl_obj[6] = new JLabel("Department Name:");  
  31.         btn_obj[0] = new JButton("Query");  
  32.         btn_obj[1] = new JButton("Clear");  
  33.         btn_obj[2] = new JButton("Next");  
  34.         btn_obj[3] = new JButton("Previous");  
  35.         btn_obj[4] = new JButton("Multiple");  
  36.         btn_obj[5] = new JButton("Done");  
  37.         btn_obj[6] = new JButton("Last");  
  38.         for (int i = 0; i < 7; i++)  
  39.         {  
  40.             tfld_obj[i] = new JTextField();  
  41.         }  
  42.         for (int i = 0; i < 7; i++) btn_obj[i].addActionListener(this);  
  43.         l1.setBounds(1003040030);  
  44.         int j = 70;  
  45.         for (int i = 0; i < 7; i++)  
  46.         {  
  47.             lbl_obj[i].setBounds(80, j, 20030);  
  48.             tfld_obj[i].setBounds(300, j, 20030);  
  49.             j = j + 40;  
  50.         }  
  51.         j = 50;  
  52.         for (int i = 0; i < 7; i++)  
  53.         {  
  54.             btn_obj[i].setBounds(j, 40010030);  
  55.             j = j + 120;  
  56.         }  
  57.         for (int i = 0; i < 7; i++)  
  58.         {  
  59.             add(lbl_obj[i]);  
  60.             add(tfld_obj[i]);  
  61.             add(btn_obj[i]);  
  62.         }  
  63.         table.setBounds(0700600700);  
  64.         add(table);  
  65.         table.setVisible(false);  
  66.         setExtendedState(JFrame.MAXIMIZED_BOTH);  
  67.         //setSize(1200, 800);  
  68.         setLayout(null);  
  69.         setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);  
  70.         setTitle("Employee Details");  
  71.         setVisible(true);  
  72.         System.out.println("Completed initialisation");  
  73.     }  
  74.     public void actionPerformed(ActionEvent e)  
  75.     {  
  76.         if (e.getSource() == btn_obj[0])  
  77.         {  
  78.             for (int i = 0; i < 7; i++)  
  79.             {  
  80.                 fld_value[i] = tfld_obj[i].getText();  
  81.             }  
  82.             try  
  83.             {  
  84.                 Class.forName("oracle.jdbc.driver.OracleDriver");  
  85.                 Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe""hr""oracle");  
  86.                 StringBuilder sqlStatement = new StringBuilder("select e.FIRST_NAME,e.last_name,e.email,e.phone_number,to_char(e.hire_date,'DD-Mon-YYYY') hire_date,e.job_id, d.department_name from employees e,departments d where e.department_id=d.department_id");  
  87.                 int flag = 0;  
  88.                 int j = 1;  
  89.                 for (j = 0; j < 7; j++)  
  90.                 {  
  91.                     if (!fld_value[j].isEmpty())  
  92.                     {  
  93.                         flag = 1;  
  94.                         break;  
  95.                     }  
  96.                 }  
  97.                 if (flag == 1)  
  98.                 {  
  99.                     if (!fld_value[0].isEmpty())  
  100.                     {  
  101.                         sqlStatement.append(" AND First_Name =\'").append(fld_value[0]).append("\'");  
  102.                     }  
  103.                     if (!fld_value[1].isEmpty())  
  104.                     {  
  105.                         sqlStatement.append(" AND last_Name = \'").append(fld_value[1]).append("\'");  
  106.                     }  
  107.                     if (!fld_value[2].isEmpty())  
  108.                     {  
  109.                         sqlStatement.append(" AND Email = \'").append(fld_value[2]).append("\'");  
  110.                     }  
  111.                     if (!fld_value[3].isEmpty())  
  112.                     {  
  113.                         sqlStatement.append(" AND phone_number = \'").append(fld_value[3]).append("\'");  
  114.                     }  
  115.                     if (!fld_value[4].isEmpty())  
  116.                     {  
  117.                         sqlStatement.append(" AND hire_date = \'").append(fld_value[4]).append("\'");  
  118.                     }  
  119.                     if (!fld_value[5].isEmpty())  
  120.                     {  
  121.                         sqlStatement.append(" AND job_id = \'").append(fld_value[4]).append("\'");  
  122.                     }  
  123.                     if (!fld_value[6].isEmpty())  
  124.                     {  
  125.                         sqlStatement.append(" AND department_name = \'").append(fld_value[4]).append("\'");  
  126.                     }  
  127.                 }  
  128.                 String sqlStmt = new String();  
  129.                 sqlStmt = sqlStatement.toString();  
  130.                 System.out.println("Sql statement generated:" + sqlStmt);  
  131.                 PreparedStatement ps = con.prepareStatement(sqlStmt, ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);  
  132.                 rs = ps.executeQuery();  
  133.                 if (rs != null)  
  134.                 {  
  135.                     rs.next();  
  136.                     for (int i = 0; i < 7; i++) tfld_obj[i].setText(rs.getString(i + 1));  
  137.                     System.out.println("First Instance");  
  138.                 }  
  139.                 System.out.println("Resulset obtained");  
  140.             }  
  141.             catch (Exception ex)  
  142.             {  
  143.                 System.out.println(ex);  
  144.             }  
  145.         }  
  146.         if (e.getSource() == btn_obj[1])  
  147.         {  
  148.             for (int i = 0; i < 7; i++) tfld_obj[i].setText("");  
  149.         }  
  150.         if (e.getSource() == btn_obj[2])  
  151.         {  
  152.             try  
  153.             {  
  154.                 if (!btn_obj[3].isEnabled()) btn_obj[3].setEnabled(true);  
  155.                 rs.next();  
  156.                 {  
  157.                     for (int i = 0; i < 7; i++) tfld_obj[i].setText(rs.getString(i + 1));  
  158.                 }  
  159.                 if (rs.isLast()) btn_obj[2].setEnabled(false);  
  160.             }  
  161.             catch (Exception ex)  
  162.             {  
  163.                 System.out.println("This is the last record");  
  164.             }  
  165.         }  
  166.         if (e.getSource() == btn_obj[3])  
  167.         {  
  168.             try  
  169.             {  
  170.                 if (!btn_obj[2].isEnabled()) btn_obj[2].setEnabled(true);  
  171.                 rs.previous();  
  172.                 {  
  173.                     for (int i = 0; i < 7; i++) tfld_obj[i].setText(rs.getString(i + 1));  
  174.                 }  
  175.                 if (rs.isFirst()) btn_obj[3].setEnabled(false);  
  176.             }  
  177.             catch (Exception ex)  
  178.             {  
  179.                 System.out.println("This is the first record");  
  180.             }  
  181.         }  
  182.         if (e.getSource() == btn_obj[4])  
  183.         {  
  184.             try  
  185.             {  
  186.                 System.out.println("In multiple mode");  
  187.                 if (btn_obj[4].getText().equals("Multiple"))  
  188.                 {  
  189.                     System.out.println("trying to initialise Jtable");  
  190.                     table = new JTable(buildTableModel(rs));  
  191.                     table.setVisible(true);  
  192.                     btn_obj[4].setText("Single");  
  193.                 }  
  194.                 else  
  195.                 {  
  196.                     table.setVisible(false);  
  197.                     btn_obj[4].setText("Multiple");  
  198.                 }  
  199.             }  
  200.             catch (Exception ex)  
  201.             {  
  202.                 System.out.println("Error while initialising table");  
  203.             }  
  204.         }  
  205.     }  
  206.     public static DefaultTableModel buildTableModel(ResultSet rs)  
  207.     throws SQLException  
  208.     {  
  209.         ResultSetMetaData metaData = rs.getMetaData();  
  210.         // names of columns  
  211.         Vector < String > columnNames = new Vector < String > ();  
  212.         int columnCount = metaData.getColumnCount();  
  213.         for (int column = 1; column <= columnCount; column++)  
  214.         {  
  215.             columnNames.add(metaData.getColumnName(column));  
  216.         }  
  217.         // data of the table  
  218.         Vector < Vector < Object >> data = new Vector < Vector < Object >> ();  
  219.         while (rs.next())  
  220.         {  
  221.             Vector < Object > vector = new Vector < Object > ();  
  222.             for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++)  
  223.             {  
  224.                 vector.add(rs.getObject(columnIndex));  
  225.             }  
  226.             data.add(vector);  
  227.         }  
  228.         return new DefaultTableModel(data, columnNames);  
  229.     }  
  230.     public static void main(String args[])  
  231.     {  
  232.         System.out.println("Program started");  
  233.         new Registration();  
  234.     }  
  235. }