使用while循环检索结果集中的值

时间:2014-05-11 13:20:12

标签: java ms-access while-loop null resultset

我有这个方法(attendanceView())从数据库中检索值。我能够检索结果集的行数和列数。但是当我使用“while(rs.next())”时,结果集返回空值。该查询在ms访问中工作正常。程序不返回任何错误,但返回空值。我该怎么做才能解决这个问题?

这是我的atendanceView()

代码
private void attendanceView() throws ClassNotFoundException{
        try{            
            String query ="TRANSFORM ABS(a.present)" +
                "SELECT e.ID, e.firstName, e.lastName, e.position, e.rate,a.overtime " +
                "FROM employees e LEFT JOIN attendance a ON e.ID = a.empID " +
                "GROUP BY e.ID, e.firstName, e.lastName, e.position, e.rate, a.present, a.overtime " +
                "PIVOT a.dateAttended";

            Object[][] result = connectToDB2(query);
            for(int x = 0; x < result.length; x++){
                for(int y=0; y < result[x].length; y++){
                    System.out.print(result[x][y]+" ");
                }
                System.out.println();
            }

            monthlyAttendanceTable.setModel(new javax.swing.table.DefaultTableModel(
                    result, new String [] {"Employee ID","First Name","Last Name", "Position", "Rate","",""}
            )
            {
                Class[] types = new Class [] {
                    java.lang.Integer.class, java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class, java.lang.Integer.class,java.lang.Integer.class,java.lang.Integer.class,java.lang.String.class
                };
                boolean[] canEdit = new boolean [] {
                    false, false, false, false, false, false, false, false, false,false
                };

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

                public boolean isCellEditable(int rowIndex, int columnIndex) {

                    return canEdit [columnIndex];
                }
            });
        }catch (ClassNotFoundException ex) {
                Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
                ex.printStackTrace();
            } 
    }

这是连接数据库的代码

private String[][] connectToDB2(String query) throws ClassNotFoundException{
        String[][] results = null;
        try {

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                String db = "jdbc:odbc:Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=D:/EACA_AgroVentures1.accdb";
                conn = DriverManager.getConnection(db);
                stmt = conn.prepareStatement(query,ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);

                ResultSet rs = stmt.executeQuery();
                ResultSetMetaData rsm = rs.getMetaData();
                rs.beforeFirst();
                int columns = rsm.getColumnCount();
                int rows = getRowCount(rs);
                if(query.contains("PIVOT")){
                    System.out.println(query);
                    rows=getRowCount2(query);

                }else if(query.contains("GROUP BY")){
                    rows = getRowCount(query);
                }

                rs.beforeFirst();
                int rowCount = 0;
                results = new String[rows][columns];
                System.out.println(rows+" PIVOT "+columns);
                while(rs.next()){    
                   System.out.print(rowCount);
                   if(rowCount == rows){
                        return results;
                   }else{
                   for(int i = 0; i < columns; i++){
                        results[rowCount][i] = rs.getString(i+1);
                        System.out.println(rowCount+",,,,"+i+" = "+results[rowCount][i]);
                     }
                     rowCount++;
                   }
                }
                rs.getStatement().close();
                conn.close();

        } catch (SQLException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
        return results;
    }

0 个答案:

没有答案