gpt4 book ai didi

java - 我得到了 Jtable 但没有数据

转载 作者:行者123 更新时间:2023-12-02 07:24:27 28 4
gpt4 key购买 nike

Possible Duplicate:
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);

}

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

最佳答案

除非您使用支持游标的数据库,否则我会仔细查看

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

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

关于java - 我得到了 Jtable 但没有数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13737417/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com