【问题标题】:I get the Jtable but no data [duplicate]我得到了 Jtable 但没有数据 [重复]
【发布时间】:2012-12-06 05:19:16
【问题描述】:

可能重复:
I can’t get my JTable to show anything

我可以显示我的表格,但我无法在表格中获取任何数据,只有列名。我的代码如下所示。

public static DefaultTableModel buildTableModel(ResultSet rs)
            throws SQLException {

        java.sql.ResultSetMetaData metaData = rs.getMetaData();

        // names of columns
        Vector<String> columnNames = new Vector<String>();
        int columnCount = metaData.getColumnCount();
        for (int column = 1; column <= columnCount; column++) {
            columnNames.add(metaData.getColumnName(column));
        }

        // data of the table
        Vector<Vector<Object>> data = new Vector<Vector<Object>>();
        while (rs.next()) {
            Vector<Object> vector = new Vector<Object>();
            for (int columnIndex = 1; columnIndex <= columnCount; columnIndex++) {

                vector.add(rs.getObject(columnIndex));

            }
            data.add(vector);
        }

        return new DefaultTableModel(data, columnNames);

    }

我的 kaldSql 看起来像这样。

public ResultSet Hentalleordreliste(Connection con){

    ResultSet Hentalleordreliste = null;

    try {
        PreparedStatement statement = con.prepareStatement("select varebestillinger.BestillingsID, " +
                "varebestillinger.LeverandoerID, "+
                "varebestillinger.BestillingsDato, varebestillinger.LeveringsDato, "+
                "varebestillinger.BestillingsStatus, varebestillinger.ModtagetAf, "+
                "varebestillinger.ModtagelsesDato, varebestillingsliste.Vare, " +
                "varebestillingsliste.Antal from varebestillinger left outer join " +
                "varebestillingsliste on  ListeID = BestillingsID");
                ResultSet rs = statement.executeQuery();


        while(rs.next()) {
            Hentalleordreliste = rs;
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return Hentalleordreliste;


}

GUI 看起来像这样。

public GUIHentOrdre() {

    try {
        con = ks.connectNow();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }



    ResultSet rs = ks.Hentalleordreliste(con);
    GUIOrdreHandler gh = new GUIOrdreHandler();
    JTable table = null;
    try {
        table = new JTable(gh.buildTableModel(rs));
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    Handler handler = new Handler();

    JPanel info = new JPanel();
    info.setLayout(new BorderLayout());
    LavBestilling = new JButton("Lav ny bestilling");
    TableModel ClassOrVoidOrModelNameReturnsTableModel = null;
    JScrollPane scroll = new JScrollPane(table);
    info.setSize(1024, 768);
    add(scroll, BorderLayout.CENTER);
    add(LavBestilling, BorderLayout.NORTH);

    LavBestilling.addActionListener(handler);

}

如前所述,我可以获取列名,可以创建表但无法获取其中存储的任何数据。

【问题讨论】:

  • 1) 为了尽快获得更好的帮助,请发帖 SSCCE。 2) 请在代码示例上使用代码格式。
  • 你能在 data.add(vector); 处调试吗?当您添加数据时。数据是否添加成功。
  • 它从不运行 while (rs.next()) { Vector vector = new Vector(); for (int columnIndex = 1; columnIndex
  • 换句话说,它不会进入while?
  • @user1880497 如果它从未进入 while 循环,则不会添加任何数据。所以数据列表将为空。

标签: java sql swing jtable resultset


【解决方案1】:

除非您使用支持游标的数据库,否则我会仔细研究一下

while(rs.next()) {
    Hentalleordreliste = rs;
}

这表明您在尝试填充表模型之前已经用尽了结果...

【讨论】:

  • 那么你的建议是什么?
  • 如果我删除循环 og 类型,我会得到一个错误,如果我像这样输入它 if(rs.next()) { Hentalleordreliste = rs; } 我遇到同样的问题
  • 如果我删除 while 并让代码看起来像这样 ResultSet rs = statement.executeQuery(); hentalleVare = rs;
  • 那我还是什么都没得到。所以如果我像你告诉我的那样然后运行它,我只有列名但没有数据
  • 您可能会得到一个空的结果集。验证实际返回的行
猜你喜欢
  • 1970-01-01
  • 2013-05-08
  • 2020-11-26
  • 1970-01-01
  • 2019-03-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多