首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > J2SE开发 >

JTable报表中总是显示不出数据

2013-10-24 
JTable表格中总是显示不出数据只显示出空的表格,不显示数据库数据代码如下:Class.forName(com.mysql.jdbc

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");


             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;
//              Object[][] info = new Object[count][6];//这里不一定非得用数组的。改成List<E>形式的
             //改成这种形式
              List<Object[]> list=new ArrayList<Object[]>();
              
              while(rs.next()){
              
           Object[] info=new Object[6];
            info[0] = rs.getString("date")+ " " +rs.getString("time");
            info[1] = rs.getString("name");
            info[2] = rs.getString("peopleamout");
            info[3] = Integer.valueOf( rs.getInt("tableid"));
            info[4] = rs.getString("phone");
            info[5] = Integer.valueOf( rs.getInt("bookingid") );
            list.add(info);
              }
          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;
               Object[][] info = new Object[count][6];


               while(rs.next()){
               count++;
             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;
               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") );
             count++;
               }
           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);
            }

热点排行