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