我得到了Jtable但没有数据 [英] I get the Jtable but no data

查看:118
本文介绍了我得到了Jtable但没有数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Possible Duplicate:
I can’t get my JTable to show anything

我可以显示表,但表中没有任何数据,仅是列名.我的代码看起来像这样.

I can show my Table but I can't get any data in the table, just the column name. My code looks like this.

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看起来像这样.

and my kaldSql looks like this.

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看起来像这样.

and the GUI looks like this.

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

}

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

As told before, I can get the column name, I can create my table but cant get any data stored inside.

推荐答案

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

Unless you're using a database that supports cursors, I would take a look hard look at

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

这将建议您在尝试填充表模型之前已经筋疲力尽了.

Which would suggest that you're exhausted the result BEFORE you've tried to populate the table model...

这篇关于我得到了Jtable但没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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