0

可能重复:
我无法让我的 JTable 显示任何内容

我可以显示我的表格,但我无法在表格中获取任何数据,只有列名。我的代码看起来像这样。

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

}

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

4

1 回答 1

2

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

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

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

于 2012-12-06T05:39:33.337 回答