选择记录后,正确的数据不会显示并在复选框中更新

时间:2019-07-02 07:46:11

标签: java swing jdbc

我正在尝试在Java程序中使用SQL中的“ Update”语句。另外,我使用了JTable.getSelectedRow()方法将存储在特定记录中的数据加载到我的复选框中。

“英语”,“孟加拉语”,“缅甸语”,“交流”,“基本计算”和“经验”字段是通过单击复选框设置的布尔值。但是,无论何时在更改现有值后运行更新查询,我的JTable都不会显示最新的更新值。此外,如果我更新一条记录然后单击它,则该程序不会将正确的数据加载到字段和框中。

我尝试创建了一个新的JTable,并再次重写了“ Update” JButton函数。我已经检查了指定复选框的索引,但是无论如何,只要我单击记录后,它就应该被“更新”,那么更改后的复选框就不会被选中/取消选中。 Check Box not same as JTable

    public ArrayList<User> userList()
       {
           ArrayList<User> usersList = new ArrayList<>();
           try {
               Class.forName(driver);
               Connection con = DriverManager.getConnection(url, user, pass);
               String query1 = "SELECT * FROM CANDIDATEINFORMATION";
               PreparedStatement prepStat = con.prepareStatement(query1);
               ResultSet rs = prepStat.executeQuery();
               User user;

               while(rs.next()){
                   user = new User(rs.getInt(
                   "Candidate_ID"), rs.getString("candidate_Name"), rs.getString("candidate_Surname"), rs.getString("Nationality"), rs.getBoolean("english"), rs.getBoolean("bangla"), rs.getBoolean("burmese"),
                   rs.getBoolean("communication"), rs.getBoolean("basic_computing"), rs.getBoolean("experience"));
                   usersList.add(user);
               }

           } catch (Exception e) {
               JOptionPane.showMessageDialog(null, e);
           }

           return usersList;
       }

    // following method displays MySQL data into JTable
       public void Show_candidate_table(){
           ArrayList<User> list = userList();
           DefaultTableModel model = (DefaultTableModel) CandidateTable.getModel();

           Object[] row = new Object[10];
           for(int i = 0; i < list.size(); i++)
           {
               row[0] = list.get(i).getID();
               row[1] = list.get(i).getCandName();
               row[2] = list.get(i).getCandSurname();
               row[3] = list.get(i).getNationality();
               row[4] = list.get(i).getCandEnglish();
               row[5] = list.get(i).getCandBangla();
               row[6] = list.get(i).getCandBurmese();
               row[7] = list.get(i).getCandCommunicative();
               row[8] = list.get(i).getCandBasicComp();
               row[9] = list.get(i).getCandExperienced();
              // row[10] = list.get(i).getDocSuf();

               model.addRow((Object[]) row);
           }    
       }

    private void CandidateTableMouseClicked(java.awt.event.MouseEvent evt) {                                            
            // display selected record to text fields 
             // Get The Index Of The Selected Row 

            DefaultTableModel model = (DefaultTableModel) CandidateTable.getModel();
            int selectedRow = CandidateTable.getSelectedRow();

            candNameBox.setText(model.getValueAt(selectedRow, 1).toString());
            candSurnameBox.setText(model.getValueAt(selectedRow, 2).toString());

            String nationality = model.getValueAt(selectedRow, 3).toString();

            switch(nationality){
                 case "Chinese":
                     nationalityBox.setSelectedIndex(0);

                     break;
                 case "Indian":
                     nationalityBox.setSelectedIndex(1);

                     break;
                 case "Burmese":
                     nationalityBox.setSelectedIndex(2);

                     break;
                 case "Bangladeshi":
                     nationalityBox.setSelectedIndex(3);

                     break;
                 case "Singaporean":
                     nationalityBox.setSelectedIndex(4);

                     break;
                 case "Malaysian":
                     nationalityBox.setSelectedIndex(5);

                     break;

             }

             boolean english = (boolean) model.getValueAt(selectedRow, 4);
             if(english){
                 engCheckBox.setSelected(true);
             }
             boolean bangla = (boolean) model.getValueAt(selectedRow, 5);
             if(bangla){
                 bangCheckBox.setSelected(true);
             }
             boolean burmese = (boolean) model.getValueAt(selectedRow, 6);
             if(burmese){
                 burmeseCheckBox.setSelected(true);
             }
             boolean communicative = (boolean) model.getValueAt(selectedRow, 7);
             if(communicative){
                 commCheckBox.setSelected(true);
             }
             boolean bas_comp = (boolean) model.getValueAt(selectedRow, 8);
             if(bas_comp){
                 bascompCheckBox.setSelected(true);
             }
             boolean exper = (boolean) model.getValueAt(selectedRow, 9);
             if(exper){
                 experCheckBox.setSelected(true);
             }

        }              


     // the following method updates the candidate record
        private void UpdateRecordButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                   
            try {
                Class.forName(driver);
                Connection con = DriverManager.getConnection(url, user, pass);

                int row = CandidateTable.getSelectedRow();
                String val2 = (CandidateTable.getModel().getValueAt(row, 0).toString());

                String sql = "UPDATE CANDIDATEINFORMATION SET CANDIDATE_NAME=?, CANDIDATE_SURNAME=? , NATIONALITY=? , ENGLISH=? , BURMESE=? ,BANGLA=? ,COMMUNICATION=? , BASIC_COMPUTING=?, EXPERIENCE=?  where CANDIDATE_ID=" + val2;

                PreparedStatement prepStat = con.prepareStatement(sql);
                prepStat.setString(1, candNameBox.getText());
                prepStat.setString(2, candSurnameBox.getText());

                String value = nationalityBox.getSelectedItem().toString();
                prepStat.setString(3, value);

                prepStat.setBoolean(4, engCheckBox.isSelected());
                prepStat.setBoolean(5, bangCheckBox.isSelected());
                prepStat.setBoolean(6, burmeseCheckBox.isSelected());
                prepStat.setBoolean(7, commCheckBox.isSelected());
                prepStat.setBoolean(8, bascompCheckBox.isSelected());
                prepStat.setBoolean(9, experCheckBox.isSelected());

                prepStat.executeUpdate();

                DefaultTableModel model = (DefaultTableModel) CandidateTable.getModel();

                model.fireTableDataChanged();
                CandidateTable.repaint();

                JOptionPane.showMessageDialog(null, "Record updated.");


            } catch (Exception e) {
                JOptionPane.showMessageDialog(null, e);
            }

        }      

例外 更新并单击记录时,我得到了'ArrayOutOfBoundsException -1'

另外,在执行时,我收到以下消息:

Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
    at jobcandidatedatabse_ia.AddCandidateInfo.CandidateTableMouseClicked(AddCandidateInfo.java:529)
    at jobcandidatedatabse_ia.AddCandidateInfo.access$600(AddCandidateInfo.java:20)
    at jobcandidatedatabse_ia.AddCandidateInfo$7.mouseClicked(AddCandidateInfo.java:226)
    at java.awt.AWTEventMulticaster.mouseClicked(AWTEventMulticaster.java:270)
    at java.awt.Component.processMouseEvent(Component.java:6536)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6298)
    at java.awt.Container.processEvent(Container.java:2236)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2294)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4888)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4534)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4466)
    at java.awt.Container.dispatchEventImpl(Container.java:2280)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:758)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:90)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.awt.EventQueue$4.run(EventQueue.java:729)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:80)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:728)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案