【发布时间】:2017-05-31 14:35:39
【问题描述】:
我正在尝试将数据库表中的所有记录显示到 JTable 中。问题是当我运行代码时,它只会显示表中的最后一条记录。
代码:
public uitgifteInfo() throws SQLException{
final JFrame frame = new JFrame("Uitgifte punten");
String[] columns = {"Nummer", "Adres", "Postcode", "Plaats",
"capaciteit"};
String sql = "SELECT * FROM uitgiftepunt";
try (
Connection conn = connection.getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql);
){
while (rs.next()) {
int nummer = rs.getObject("nummer", Integer.class);
String adres = rs.getObject("adres", String.class);
String postcode = rs.getObject("postcode", String.class);
String plaats = rs.getObject("plaats", String.class);
int cap = rs.getObject("capaciteit", Integer.class);
Object[][] data = {
{nummer,adres,postcode,plaats,cap}
};
JTable table = new JTable(data, columns);
JScrollPane scrollPane = new JScrollPane(table);
table.setFillsViewportHeight(true);
JLabel lblHeading = new JLabel("Uitgiftepunt Info");
lblHeading.setFont(new Font("Arial",Font.TRUETYPE_FONT,24));
frame.getContentPane().setLayout(new BorderLayout());
frame.getContentPane().add(lblHeading,BorderLayout.PAGE_START);
frame.getContentPane().add(scrollPane,BorderLayout.CENTER);
}
}
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(550, 550);
frame.setVisible(true);
}
object[][] 数据不应该显示我表中的所有记录吗?而且不仅仅是最后一个。
【问题讨论】:
-
将每个一维对象数组添加到列表中,并在while循环之后将其添加到表中