JTable仅显示表中的最后一条记录 [英] JTable only displays last record from table

查看:80
本文介绍了JTable仅显示表中的最后一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将数据库表中的所有记录显示到JTable中.问题是当我运行代码时,它将仅显示表中的最后一条记录.

I'm trying to display all the records from my database table into an JTable. The problem is when I run the code it will only display the last record from the table.

代码:

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 [] []数据不是应该显示表中的所有记录吗?而不仅仅是最后一个.

Isn't object[][] data supposed to show all the records from my table? And not only the last one.

推荐答案

object [] []数据不是应该显示表中的所有记录吗?而且不仅是最后一个

Isn't object[][] data supposed to show all the records from my table? And not only the last one

是的,但是您要为ResultSet中的每一行创建一个新的2D数组和一个新的JTable.

Yes, but you create a new 2D array and a new JTable for every row in the ResultSet.

相反,您需要在循环开始之前创建一个空的DefaultTableModel,然后在循环内使用addRow(...)方法将ResultSet中的数据行添加到表模型中.

Instead you need to create an empty DefaultTableModel before the loop starts, Then inside the loop you use the addRow(...) method to add the row of data from the ResultSet to the table model.

然后,当循环结束时,您将使用表模型创建表.

Then when the loop is finished, you create the table using the table model.

所以基本结构是:

DefaultTableModel model = new DefaultTableModel(columnNames, 0);

while (rs.next())
{
    ....
    model.addRow(...);
}

JTable table = new JTable(model);
JScrollPane scrollPane = new JScrollPane(table);
...

这篇关于JTable仅显示表中的最后一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆