SHARE
TWEET

MySampleCrud

a guest Apr 18th, 2019 87 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * To change this license header, choose License Headers in Project Properties.
  3.  * To change this template file, choose Tools | Templates
  4.  * and open the template in the editor.
  5.  */
  6. package person;
  7. import java.awt.Component;
  8. import java.sql.*;
  9. import java.time.LocalDate;
  10. import java.util.logging.Level;
  11. import java.util.logging.Logger;
  12. import javax.swing.*;
  13. import javax.swing.table.DefaultTableModel;
  14. import javax.swing.table.TableModel;
  15. import net.proteanit.sql.DbUtils;
  16.  
  17.  
  18.  
  19. public class Employee extends javax.swing.JFrame {
  20.  
  21.     Connection con;
  22.     Statement smt;
  23.     PreparedStatement pst;
  24.     ResultSet rs;
  25.    
  26.     public Employee() {
  27.         initComponents();
  28.         con = DBConnection.ConnectionDB();
  29.         Populate_jTable();
  30.        
  31.     }
  32.    
  33.     public void Populate_jTable(){
  34.        
  35.        try{
  36.            String FetchData = "Select * from person";
  37.             pst = con.prepareStatement(FetchData);
  38.             rs = pst.executeQuery();
  39.             DBTable.setModel(DbUtils.resultSetToTableModel(rs));
  40.        }catch (SQLException ex) {
  41.             Logger.getLogger(Employee.class.getName()).log(Level.SEVERE,null,ex);
  42.         }
  43.     }
  44.    
  45.  
  46.     /**
  47.      * This method is called from within the constructor to initialize the form.
  48.      * WARNING: Do NOT modify this code. The content of this method is always
  49.      * regenerated by the Form Editor.
  50.      */
  51.     @SuppressWarnings("unchecked")
  52.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
  53.     private void initComponents() {
  54.  
  55.         jButton4 = new javax.swing.JButton();
  56.         buttonGroupGender = new javax.swing.ButtonGroup();
  57.         buttonGroupMaritalStatus = new javax.swing.ButtonGroup();
  58.         jPanel1 = new javax.swing.JPanel();
  59.         jLabel2 = new javax.swing.JLabel();
  60.         jLabel3 = new javax.swing.JLabel();
  61.         jLabel4 = new javax.swing.JLabel();
  62.         jTextFieldFName = new javax.swing.JTextField();
  63.         jTextFieldLName = new javax.swing.JTextField();
  64.         jTextFieldAddress = new javax.swing.JTextField();
  65.         jButtonAdd = new javax.swing.JButton();
  66.         jButtonDelete = new javax.swing.JButton();
  67.         jButtonEdit = new javax.swing.JButton();
  68.         jButtonClear = new javax.swing.JButton();
  69.         jPanel3 = new javax.swing.JPanel();
  70.         jTextFieldID = new javax.swing.JTextField();
  71.         jLabel5 = new javax.swing.JLabel();
  72.         jRadioButtonMale = new javax.swing.JRadioButton();
  73.         jRadioButtonFemale = new javax.swing.JRadioButton();
  74.         jLabel6 = new javax.swing.JLabel();
  75.         datePicker = new com.github.lgooddatepicker.components.DatePicker();
  76.         jScrollPane1 = new javax.swing.JScrollPane();
  77.         DBTable = new javax.swing.JTable();
  78.         jRadioButtonSingle = new javax.swing.JRadioButton();
  79.         jRadioButtonMarried = new javax.swing.JRadioButton();
  80.         jRadioButtonDivorced = new javax.swing.JRadioButton();
  81.  
  82.         jButton4.setText("jButton4");
  83.  
  84.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  85.  
  86.         jPanel1.setBorder(javax.swing.BorderFactory.createEtchedBorder());
  87.  
  88.         jLabel2.setText("First Name");
  89.  
  90.         jLabel3.setText("Last Name");
  91.  
  92.         jLabel4.setText("Address");
  93.  
  94.         jTextFieldLName.addActionListener(new java.awt.event.ActionListener() {
  95.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  96.                 jTextFieldLNameActionPerformed(evt);
  97.             }
  98.         });
  99.  
  100.         jButtonAdd.setText("Add");
  101.         jButtonAdd.addActionListener(new java.awt.event.ActionListener() {
  102.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  103.                 jButtonAddActionPerformed(evt);
  104.             }
  105.         });
  106.  
  107.         jButtonDelete.setText("Del");
  108.         jButtonDelete.addActionListener(new java.awt.event.ActionListener() {
  109.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  110.                 jButtonDeleteActionPerformed(evt);
  111.             }
  112.         });
  113.  
  114.         jButtonEdit.setText("Update");
  115.         jButtonEdit.addActionListener(new java.awt.event.ActionListener() {
  116.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  117.                 jButtonEditActionPerformed(evt);
  118.             }
  119.         });
  120.  
  121.         jButtonClear.setText("Clear");
  122.         jButtonClear.addActionListener(new java.awt.event.ActionListener() {
  123.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  124.                 jButtonClearActionPerformed(evt);
  125.             }
  126.         });
  127.  
  128.         javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
  129.         jPanel3.setLayout(jPanel3Layout);
  130.         jPanel3Layout.setHorizontalGroup(
  131.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  132.             .addGap(0, 0, Short.MAX_VALUE)
  133.         );
  134.         jPanel3Layout.setVerticalGroup(
  135.             jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  136.             .addGap(0, 0, Short.MAX_VALUE)
  137.         );
  138.  
  139.         jTextFieldID.setEnabled(false);
  140.         jTextFieldID.addActionListener(new java.awt.event.ActionListener() {
  141.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  142.                 jTextFieldIDActionPerformed(evt);
  143.             }
  144.         });
  145.  
  146.         jLabel5.setText("ID");
  147.  
  148.         buttonGroupGender.add(jRadioButtonMale);
  149.         jRadioButtonMale.setText("Male");
  150.         jRadioButtonMale.addActionListener(new java.awt.event.ActionListener() {
  151.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  152.                 jRadioButtonMaleActionPerformed(evt);
  153.             }
  154.         });
  155.  
  156.         buttonGroupGender.add(jRadioButtonFemale);
  157.         jRadioButtonFemale.setText("Female");
  158.         jRadioButtonFemale.addActionListener(new java.awt.event.ActionListener() {
  159.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  160.                 jRadioButtonFemaleActionPerformed(evt);
  161.             }
  162.         });
  163.  
  164.         jLabel6.setText("Date Of Birth");
  165.  
  166.         datePicker.setPreferredSize(new java.awt.Dimension(120, 18));
  167.  
  168.         DBTable.setModel(new javax.swing.table.DefaultTableModel(
  169.             new Object [][] {
  170.                 {null, null, null, null, null, null, null},
  171.                 {null, null, null, null, null, null, null},
  172.                 {null, null, null, null, null, null, null},
  173.                 {null, null, null, null, null, null, null}
  174.             },
  175.             new String [] {
  176.                 "ID", "First Name", "Last Name", "Address", "Gender", "Date Of Birth", "Marital Status"
  177.             }
  178.         ) {
  179.             boolean[] canEdit = new boolean [] {
  180.                 false, false, false, false, false, false, false
  181.             };
  182.  
  183.             public boolean isCellEditable(int rowIndex, int columnIndex) {
  184.                 return canEdit [columnIndex];
  185.             }
  186.         });
  187.         DBTable.addMouseListener(new java.awt.event.MouseAdapter() {
  188.             public void mouseClicked(java.awt.event.MouseEvent evt) {
  189.                 DBTableMouseClicked(evt);
  190.             }
  191.         });
  192.         jScrollPane1.setViewportView(DBTable);
  193.  
  194.         buttonGroupMaritalStatus.add(jRadioButtonSingle);
  195.         jRadioButtonSingle.setText("Single");
  196.  
  197.         buttonGroupMaritalStatus.add(jRadioButtonMarried);
  198.         jRadioButtonMarried.setText("Married");
  199.         jRadioButtonMarried.addActionListener(new java.awt.event.ActionListener() {
  200.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  201.                 jRadioButtonMarriedActionPerformed(evt);
  202.             }
  203.         });
  204.  
  205.         buttonGroupMaritalStatus.add(jRadioButtonDivorced);
  206.         jRadioButtonDivorced.setText("Divorced");
  207.  
  208.         javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
  209.         jPanel1.setLayout(jPanel1Layout);
  210.         jPanel1Layout.setHorizontalGroup(
  211.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  212.             .addGroup(jPanel1Layout.createSequentialGroup()
  213.                 .addContainerGap()
  214.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  215.                     .addComponent(jLabel5, javax.swing.GroupLayout.PREFERRED_SIZE, 19, javax.swing.GroupLayout.PREFERRED_SIZE)
  216.                     .addGroup(jPanel1Layout.createSequentialGroup()
  217.                         .addGap(44, 44, 44)
  218.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  219.                             .addGroup(jPanel1Layout.createSequentialGroup()
  220.                                 .addComponent(jButtonAdd, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  221.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  222.                                 .addComponent(jButtonEdit))
  223.                             .addGroup(jPanel1Layout.createSequentialGroup()
  224.                                 .addComponent(jButtonDelete, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE)
  225.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  226.                                 .addComponent(jButtonClear, javax.swing.GroupLayout.PREFERRED_SIZE, 67, javax.swing.GroupLayout.PREFERRED_SIZE))))
  227.                     .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  228.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  229.                             .addComponent(jLabel4)
  230.                             .addComponent(jLabel3)
  231.                             .addComponent(jLabel2))
  232.                         .addGap(27, 27, 27)
  233.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
  234.                             .addComponent(jTextFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
  235.                             .addComponent(jTextFieldLName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
  236.                             .addComponent(jTextFieldFName, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE)
  237.                             .addComponent(jTextFieldID, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 58, javax.swing.GroupLayout.PREFERRED_SIZE)))
  238.                     .addGroup(jPanel1Layout.createSequentialGroup()
  239.                         .addGap(54, 54, 54)
  240.                         .addComponent(jRadioButtonMale, javax.swing.GroupLayout.PREFERRED_SIZE, 72, javax.swing.GroupLayout.PREFERRED_SIZE)
  241.                         .addGap(27, 27, 27)
  242.                         .addComponent(jRadioButtonFemale))
  243.                     .addGroup(jPanel1Layout.createSequentialGroup()
  244.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  245.                             .addComponent(jLabel6)
  246.                             .addComponent(jRadioButtonSingle))
  247.                         .addGap(18, 18, 18)
  248.                         .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  249.                             .addGroup(jPanel1Layout.createSequentialGroup()
  250.                                 .addGap(0, 0, Short.MAX_VALUE)
  251.                                 .addComponent(jRadioButtonMarried)
  252.                                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  253.                                 .addComponent(jRadioButtonDivorced))
  254.                             .addComponent(datePicker, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  255.                 .addGap(18, 18, 18)
  256.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 902, javax.swing.GroupLayout.PREFERRED_SIZE)
  257.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  258.                 .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  259.                 .addContainerGap())
  260.         );
  261.         jPanel1Layout.setVerticalGroup(
  262.             jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  263.             .addGroup(jPanel1Layout.createSequentialGroup()
  264.                 .addGap(19, 19, 19)
  265.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  266.                     .addComponent(jLabel5)
  267.                     .addComponent(jTextFieldID, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  268.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  269.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  270.                     .addComponent(jLabel2)
  271.                     .addComponent(jTextFieldFName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  272.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  273.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  274.                     .addComponent(jLabel3)
  275.                     .addComponent(jTextFieldLName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  276.                 .addGap(17, 17, 17)
  277.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  278.                     .addComponent(jLabel4)
  279.                     .addComponent(jTextFieldAddress, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  280.                 .addGap(18, 18, 18)
  281.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  282.                     .addComponent(jRadioButtonMale, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
  283.                     .addComponent(jRadioButtonFemale, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
  284.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  285.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  286.                     .addComponent(jRadioButtonSingle)
  287.                     .addComponent(jRadioButtonMarried)
  288.                     .addComponent(jRadioButtonDivorced))
  289.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  290.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  291.                     .addComponent(datePicker, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
  292.                     .addComponent(jLabel6))
  293.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  294.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  295.                     .addComponent(jButtonAdd)
  296.                     .addComponent(jButtonEdit))
  297.                 .addGap(9, 9, 9)
  298.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  299.                     .addComponent(jButtonDelete)
  300.                     .addComponent(jButtonClear))
  301.                 .addGap(54, 54, 54))
  302.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel1Layout.createSequentialGroup()
  303.                 .addContainerGap()
  304.                 .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  305.                     .addComponent(jPanel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  306.                     .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 496, Short.MAX_VALUE))
  307.                 .addContainerGap())
  308.         );
  309.  
  310.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  311.         getContentPane().setLayout(layout);
  312.         layout.setHorizontalGroup(
  313.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  314.             .addGroup(layout.createSequentialGroup()
  315.                 .addContainerGap()
  316.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  317.                 .addContainerGap())
  318.         );
  319.         layout.setVerticalGroup(
  320.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  321.             .addGroup(layout.createSequentialGroup()
  322.                 .addContainerGap()
  323.                 .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  324.                 .addContainerGap())
  325.         );
  326.  
  327.         pack();
  328.     }// </editor-fold>                        
  329.  
  330.     private void jTextFieldLNameActionPerformed(java.awt.event.ActionEvent evt) {                                                
  331.         // TODO add your handling code here:
  332.     }                                              
  333.  
  334.     public void Clear_Fields(){
  335.         jTextFieldID.setText("");
  336.         jTextFieldFName.setText("");
  337.         jTextFieldLName.setText("");
  338.         jTextFieldAddress.setText("");
  339.         buttonGroupGender.clearSelection(); // this will clear the radio buttons after inser update or delete.
  340.         datePicker.clear();
  341.         buttonGroupMaritalStatus.clearSelection();
  342.     }
  343.     private void jButtonClearActionPerformed(java.awt.event.ActionEvent evt) {                                            
  344.         // TODO add your handling code here:  
  345.         Clear_Fields();
  346.     }                                            
  347.  
  348.     private void jButtonAddActionPerformed(java.awt.event.ActionEvent evt) {                                          
  349.        
  350.         int InsertDB = JOptionPane.showConfirmDialog(null,"Do you want to save this record?", "SAVE",JOptionPane.YES_NO_OPTION);
  351.        
  352.         if(InsertDB == 0){
  353.            
  354.         String MaritalStatus = null;
  355.         String Gend = null;
  356.         if(jRadioButtonMale.isSelected())
  357.         {
  358.             Gend = "Male";
  359.         }else if(jRadioButtonFemale.isSelected())
  360.         {
  361.             Gend = "Female";
  362.         }
  363.         if(jRadioButtonSingle.isSelected())
  364.         {
  365.             MaritalStatus = "Single";
  366.         }
  367.         else if(jRadioButtonMarried.isSelected())
  368.         {
  369.             MaritalStatus = "Married";
  370.         }
  371.         else if(jRadioButtonDivorced.isSelected())
  372.         {
  373.             MaritalStatus = "Divorced";
  374.         }        
  375.          if(datePicker.getDate() == null)
  376.         {
  377.             datePicker.setDateToToday();
  378.         }
  379.        
  380.        
  381.         DefaultTableModel Model = (DefaultTableModel) DBTable.getModel();
  382.         Model.addRow(new Object[]{jTextFieldFName.getText(), jTextFieldLName.getText(), jTextFieldAddress.getText()});
  383.        
  384.         String FirstName = jTextFieldFName.getText();
  385.         String LastName = jTextFieldLName.getText();
  386.         String Address = jTextFieldAddress.getText();
  387.         LocalDate Date = datePicker.getDate();
  388.        
  389.        
  390.         try(Statement stmt = con.createStatement()){
  391.            
  392.             String insertToDB  = "INSERT INTO person (FirstName, LastName, Address, Gender, DateOfBirth, MaritalStatus) VALUES ('"+jTextFieldFName.getText()+"','"+jTextFieldLName.getText()+"',"
  393.                     + "'"+jTextFieldAddress.getText()+"','"+Gend+"', '"+datePicker.getDate()+"','"+MaritalStatus+"')";
  394.             stmt.execute(insertToDB);
  395.             Component frame = null;
  396.             JOptionPane.showMessageDialog(frame, "Successfully Saved");
  397.            
  398.         } catch (SQLException ex) {
  399.             Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
  400.         }
  401.        
  402.         Populate_jTable();
  403.         Clear_Fields();
  404.         }else
  405.         {
  406.             Clear_Fields();
  407.         }
  408.     }                                          
  409.  
  410.     private void jButtonDeleteActionPerformed(java.awt.event.ActionEvent evt) {                                              
  411.        
  412.         int del = JOptionPane.showConfirmDialog(null, "Do you want to delete this record?","Delete",JOptionPane.YES_NO_OPTION);
  413.         if(del == 0){
  414.         try(Statement stmt = con.createStatement()){          
  415.             String DeleteFromDB = "Delete FROM person WHERE ID = '"+jTextFieldID.getText()+"'";
  416.             stmt.execute(DeleteFromDB);
  417.             Component frame = null;
  418.             JOptionPane.showMessageDialog(frame, "Successfully Deleted");
  419.         }catch (SQLException ex) {
  420.             Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
  421.         }
  422.         Populate_jTable();
  423.         Clear_Fields();
  424.         }
  425.     }                                            
  426.  
  427.     private void DBTableMouseClicked(java.awt.event.MouseEvent evt) {                                    
  428.                
  429.         int GetData = DBTable.getSelectedRow();
  430.         TableModel Model = DBTable.getModel();
  431.         jTextFieldID.setText(Model.getValueAt(GetData, 0).toString());
  432.         jTextFieldFName.setText(Model.getValueAt(GetData, 1).toString());
  433.         jTextFieldLName.setText(Model.getValueAt(GetData, 2).toString());
  434.         jTextFieldAddress.setText(Model.getValueAt(GetData, 3).toString());
  435.         datePicker.setText(Model.getValueAt(GetData, 5).toString());
  436.        
  437.                
  438.         switch (Model.getValueAt(GetData, 4).toString()) {
  439.             case "Male":
  440.                 jRadioButtonMale.setSelected(true);
  441.                 break;
  442.             case "Female":
  443.                 jRadioButtonFemale.setSelected(true);
  444.                 break;
  445.             default:
  446.                 buttonGroupGender.clearSelection();
  447.                 break;
  448.         }
  449.        
  450.                 switch (Model.getValueAt(GetData, 6).toString()) {
  451.             case "Single":
  452.                 jRadioButtonSingle.setSelected(true);
  453.                 break;
  454.             case "Married":
  455.                 jRadioButtonMarried.setSelected(true);
  456.                 break;
  457.             case "Divorced":
  458.                 jRadioButtonDivorced.setSelected(true);
  459.                 break;
  460.             default:
  461.                 buttonGroupMaritalStatus.clearSelection();
  462.                 break;
  463.  
  464.        
  465.        
  466.         /*if(Model.getValueAt(GetData, 6).toString().equals("Single"))
  467.         {
  468.         jRadioButtonSingle.setSelected(true);
  469.         }
  470.         else if(Model.getValueAt(GetData, 6).toString().equals("Married"))
  471.         {
  472.         jRadioButtonMarried.setSelected(true);
  473.         }
  474.         else if(Model.getValueAt(GetData, 6).toString().equals("Divorced"))
  475.         {
  476.         jRadioButtonDivorced.setSelected(true);
  477.         }
  478.         else
  479.         {
  480.         buttonGroupMaritalStatus.clearSelection();
  481.         }*/
  482.         /*if((Model.getValueAt(GetData, 4).toString()).equals("Male"))
  483.         {
  484.         jRadioButtonMale.setSelected(true);
  485.         }
  486.         else if((Model.getValueAt(GetData, 4).toString()).equals("Female"))
  487.         {
  488.         jRadioButtonFemale.setSelected(true);
  489.         }
  490.         else
  491.         {
  492.         buttonGroupGender.clearSelection();
  493.         }*/
  494.         }
  495.     }                                    
  496.  
  497.     private void jButtonEditActionPerformed(java.awt.event.ActionEvent evt) {                                            
  498.  
  499.         int update = JOptionPane.showConfirmDialog(null, "Do you want to update this record?","Update",JOptionPane.YES_NO_OPTION);
  500.        
  501.         if(update==0){
  502.          
  503.         String Gend = "", MaritalStat = "";
  504.            
  505.         if(jRadioButtonMale.isSelected())
  506.         {
  507.             Gend = "Male";
  508.         }
  509.         else if(jRadioButtonFemale.isSelected())
  510.         {
  511.             Gend = "Female";
  512.         }  
  513.        
  514.         if (jRadioButtonSingle.isSelected())
  515.         {
  516.             MaritalStat = "Single";
  517.         }
  518.         else if (jRadioButtonMarried.isSelected())
  519.         {
  520.             MaritalStat = "Married";
  521.         }
  522.         else if (jRadioButtonDivorced.isSelected())
  523.         {
  524.             MaritalStat = "Divorced";
  525.         }
  526.        
  527.         if (datePicker == null){
  528.             datePicker.setDateToToday();
  529.         }
  530.        
  531.         try(Statement stmt = con.createStatement()){
  532.             String UpdateDB = ("UPDATE person SET ID ='"+jTextFieldID.getText()+"',FirstName ='"+jTextFieldFName.getText()
  533.                     +"',LastName ='"+jTextFieldLName.getText()+"',Address ='"+jTextFieldAddress.getText()+"'"
  534.                     + " Gender ='"+Gend+"', DateOfBirth = '"+datePicker.getDate()+"', MaritalStatus = '"+MaritalStat+"' WHERE ID ='"+jTextFieldID.getText()+"'" );
  535.             stmt.executeUpdate(UpdateDB);
  536.             Component frame = null;
  537.             JOptionPane.showMessageDialog(frame, "Data is successfully updated");
  538.         }catch (SQLException ex) {
  539.             Logger.getLogger(Employee.class.getName()).log(Level.SEVERE, null, ex);
  540.         }
  541.         Populate_jTable();
  542.         }
  543.     }                                          
  544.  
  545.     private void jTextFieldIDActionPerformed(java.awt.event.ActionEvent evt) {                                            
  546.         jTextFieldID.setEditable(false);
  547.     }                                            
  548.  
  549.    
  550.    
  551.     private void jRadioButtonMaleActionPerformed(java.awt.event.ActionEvent evt) {                                                
  552.        
  553.     }                                                
  554.  
  555.     private void jRadioButtonFemaleActionPerformed(java.awt.event.ActionEvent evt) {                                                  
  556.        
  557.     }                                                  
  558.  
  559.     private void jRadioButtonMarriedActionPerformed(java.awt.event.ActionEvent evt) {                                                    
  560.         // TODO add your handling code here:
  561.     }                                                  
  562.  
  563.    
  564.     /**
  565.      * @param args the command line arguments
  566.      */
  567.     public static void main(String args[]) {
  568.         /* Set the Nimbus look and feel */
  569.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
  570.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
  571.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
  572.          */
  573.         try {
  574.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
  575.                 if ("Nimbus".equals(info.getName())) {
  576.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());
  577.                     break;
  578.                 }
  579.             }
  580.         } catch (ClassNotFoundException ex) {
  581.             java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  582.         } catch (InstantiationException ex) {
  583.             java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  584.         } catch (IllegalAccessException ex) {
  585.             java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  586.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {
  587.             java.util.logging.Logger.getLogger(Employee.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
  588.         }
  589.         //</editor-fold>
  590.  
  591.         /* Create and display the form */
  592.         java.awt.EventQueue.invokeLater(new Runnable() {
  593.             public void run() {
  594.                 new Employee().setVisible(true);
  595.             }
  596.         });
  597.     }
  598.  
  599.     // Variables declaration - do not modify                    
  600.     private javax.swing.JTable DBTable;
  601.     private javax.swing.ButtonGroup buttonGroupGender;
  602.     private javax.swing.ButtonGroup buttonGroupMaritalStatus;
  603.     private com.github.lgooddatepicker.components.DatePicker datePicker;
  604.     private javax.swing.JButton jButton4;
  605.     private javax.swing.JButton jButtonAdd;
  606.     private javax.swing.JButton jButtonClear;
  607.     private javax.swing.JButton jButtonDelete;
  608.     private javax.swing.JButton jButtonEdit;
  609.     private javax.swing.JLabel jLabel2;
  610.     private javax.swing.JLabel jLabel3;
  611.     private javax.swing.JLabel jLabel4;
  612.     private javax.swing.JLabel jLabel5;
  613.     private javax.swing.JLabel jLabel6;
  614.     private javax.swing.JPanel jPanel1;
  615.     private javax.swing.JPanel jPanel3;
  616.     private javax.swing.JRadioButton jRadioButtonDivorced;
  617.     private javax.swing.JRadioButton jRadioButtonFemale;
  618.     private javax.swing.JRadioButton jRadioButtonMale;
  619.     private javax.swing.JRadioButton jRadioButtonMarried;
  620.     private javax.swing.JRadioButton jRadioButtonSingle;
  621.     private javax.swing.JScrollPane jScrollPane1;
  622.     private javax.swing.JTextField jTextFieldAddress;
  623.     private javax.swing.JTextField jTextFieldFName;
  624.     private javax.swing.JTextField jTextFieldID;
  625.     private javax.swing.JTextField jTextFieldLName;
  626.     // End of variables declaration                  
  627.    
  628. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top