日期:2014-05-20  浏览次数:20659 次

JTable表格中总是显示不出数据
只显示出空的表格,不显示数据库数据
代码如下:
          Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/tablesys?" + "user=test&password=123");
            preparedStatement = connect.prepareStatement("select * from tablesys.booking ");
            ResultSet rs = preparedStatement.executeQuery();
               int count = 0;
               while(rs.next()){
               count++;
                 }
             Object[][] info = new Object[count][6];
            while(rs.next()){
             info[count][0] = rs.getString("date")+ " " +rs.getString("time");
             info[count][1] = rs.getString("name");
             info[count][2] = rs.getString("peopleamout");
             info[count][3] = Integer.valueOf( rs.getInt("tableid"));
             info[count][4] = rs.getString("phone");
             info[count][5] = Integer.valueOf( rs.getInt("bookingid") );
               }
           MyTableModel model = new MyTableModel(info, new String[]{
                            "Time", "Name", "Covers", "Table No", "Phone", "BookingID"
                        });
           jTable1.setModel(model);
           
            } catch (Exception ex) {
                Logger.getLogger(MainMenu.class.getName()).log(Level.SEVERE, null, ex);
            }

------解决方案--------------------

         Class.forName("com.mysql.jdbc.Driver");
            connect = DriverManager.getConnection("jdbc:mysql://localhost/tablesys?" + "user=test&password=123");
            preparedStatement = connect.prepareStatement("select * from tablesys.booking ");
            ResultSet rs = preparedStatement.executeQuery();
               int count = 0;
               while(rs.next()){// 这里已经迭代完了,所以下面2处没有了
               count++;
                 }
             Object[][] info = new Object[count][6];
            while(rs.next()){//这里没有进入
             info[count][0] = rs.getString("date")+ " " +rs.getString("time");
             info[count][1] = rs.getString("name");
             info[count][2] = rs.getString("peopleamout");
             info[count][3] = Integer.valueOf( rs.getInt("tableid"));
             info[count][4] = rs.getString("phone");