使用数据库连接解决IllegalArgumentException

时间:2014-05-01 18:29:01

标签: java for-loop jtable database-connection illegalargumentexception

请帮忙。我有这个程序连接到数据库。它完美连接,但执行时这段代码包含IllegalArgumentException。此方法应从我的数据库中检索数据,然后将其放入jtable中。首先,它工作正常(将检索到的数据从数据库放入jtable)但是当我添加" for-loop"为了迭代jtable中每个单元格中的数据,这也执行了已选中复选框的添加,会发生这种错误。我该怎么做才能解决这个问题?谢谢你提前。

private void loadAttendance(){
    int counter = 0;
    try {
        String query = "SELECT ID,firstName,lastName,position,rate,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15 FROM employees";            
        Object[][] result = connectToDB(query);

        Calendar cal = Calendar.getInstance();
        int val = cal.get(Calendar.DAY_OF_MONTH);
        System.out.println(val);
        String [] dates;

        if(val >= 1 && val <=15){
            attendanceTable.setModel(new javax.swing.table.DefaultTableModel(
            result, dates = new String [] {"ID","First Name", "Last Name", "Position","Daily Rate","1","2","3","4","5","6","7","8","9","10","11","12","13","14","15", "Total", "Gross Pay", "Vale","Net Pay"}
        ) 
        {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Double.class,java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };

            boolean[] canEdit = new boolean [] {
                false, true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
            for (int i = 0 ; i < attendanceTable.getRowCount(); i++){
                for (int j = 4 ; j <= 14; j++){
                    result[i][j] = attendanceTable.getValueAt(i,j);
                    System.out.println(" " + i + " " + j + " " + result[i][j]);
                    if(result[i][j] != null && result[i][j].equals(true)){
                        counter++;
                    }
                }
               System.out.println(counter);
               attendanceTable.setValueAt(counter,i,19);
               counter = 0;

               int a = (Integer) attendanceTable.getValueAt(i,19);
               System.out.println(a+"  -value for days");

               double d = Double.parseDouble(String.valueOf(attendanceTable.getValueAt(i,5)));
               System.out.println(d+"   -value for daily rate");

               double product = a*d;
               attendanceTable.setValueAt(product,i,20);
               System.out.println("sample"+product);
            }
        }else{
            attendanceTable.setModel(new javax.swing.table.DefaultTableModel(
            result, dates = new String [] {"ID","First Name", "Last Name", "Position", "16","17","18","19","20","21","22","23","24","25","26","27","28","29","30","31", "Total", "Daily Rate","Gross Pay", "Vale","Net Pay"}
        ) 
        {
            Class[] types = new Class [] {
                java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Boolean.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class, java.lang.Double.class
            };
            boolean[] canEdit = new boolean [] {
               false, true, true, false, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, true, false, true, false, true, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        }); 
            for (int i = 0 ; i < attendanceTable.getRowCount(); i++){
                for (int j = 0 ; j <= attendanceTable.getColumnCount(); j++){
                    result[i][j] = attendanceTable.getValueAt(i,j);
                    System.out.println(" " + i + " " + j + " " + result[i][j]);
                    if(result[i][j] != null && result[i][j].equals(true)){
                        counter++;
                    }
                }
               System.out.println(counter);
               attendanceTable.setValueAt(counter,i,19);
               counter = 0;

               Integer a = (Integer) attendanceTable.getValueAt(i,19);
               System.out.println(a+"  -value for days");

               Double d =(Double) attendanceTable.getValueAt(i,20);
               System.out.println(d+"   -value for daily rate");

               Double product = a*d;
               attendanceTable.setValueAt(product,i,21);
            }
        }
        for(int x = 0; x <= dates.length-1; x++){
            System.out.println(dates[x]);
        }
    } catch (ClassNotFoundException ex) {
        Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
    }
}

0 个答案:

没有答案