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

小菜鸟的成长 2013-10-22 06:06:48
只显示出空的表格,不显示数据库数据
代码如下:
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);
}
...全文
494 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
小菜鸟的成长 2013-10-23
  • 打赏
  • 举报
回复
5# MyTableModel model = new MyTableModel(info, new String[] 中info显示未定义。如果把 Object[] info=new Object[6];移到循环的外面,还是错误。 对于MyTableModel(Object[],String[]), 找不到合适的构造器 构造器 MyTableModel.MyTableModel(Object[][],String[])不适用 (参数不匹配; Object[]无法转换为Object[][]) 构造器 MyTableModel.MyTableModel(Vector,Vector)不适用 (参数不匹配; Object[]无法转换为Vector)
小菜鸟的成长 2013-10-23
  • 打赏
  • 举报
回复
6#跟1#结果一样错误
小菜鸟的成长 2013-10-23
  • 打赏
  • 举报
回复
1#的代码连表格都出不来了。 不过原因应该正确
teemai 2013-10-23
  • 打赏
  • 举报
回复

         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();
               rs.last(); //移到最后一行
               int count = rs.getRow(); //得到当前行号,也就是记录数
               rs.beforeFirst(); //还要用到记录集,就把指针再移到初始化的位置
             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);
            } 
Defonds 2013-10-22
  • 打赏
  • 举报
回复
引用 4 楼 defonds 的回复:
    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);
            }
失落夏天 2013-10-22
  • 打赏
  • 举报
回复
错误就是二楼说错的那个错误,但是方法可以这样改。 另外,记得以前有人没加列名而导致没有成功的输入数据,楼主如果遇到这种问题的话记得加上列名。

 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);
           } 
Defonds 2013-10-22
  • 打赏
  • 举报
回复
    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);
            }
teemai 2013-10-22
  • 打赏
  • 举报
回复
试试上面的的代码
teemai 2013-10-22
  • 打赏
  • 举报
回复

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];
            rs = preparedStatement.executeQuery();
            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);
            } 
teemai 2013-10-22
  • 打赏
  • 举报
回复

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

62,614

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧