gpt4 book ai didi

java - 跳过结果集中的第一行到 JTABLE 显示

转载 作者:行者123 更新时间:2023-12-01 10:13:40 26 4
gpt4 key购买 nike

我想在使用 dbutil 时在 jtable 中显示数据,它会跳过第一行并显示其他行,这里是代码:我尝试使用 rs.first() 进行双循环,但没有成功

int id=0,quantite=0;
double prix=0.0;
ResultSet rs=null;
String sql = "Select id,prix,quantite from commande where quantite="+Integer.parseInt(tfRecherche.getText());
CommandeDao commande = new CommandeDao();
Statement st = commande.getSt();
try {
rs= st.executeQuery(sql);
while(rs.next()){
if(rs.isFirst()){
System.out.println("first");
}
id= rs.getInt("id");
quantite = rs.getInt("quantite");
prix = rs.getDouble("prix");
tbAffichage.setModel(DbUtils.resultSetToTableModel(rs));
}
} catch (SQLException ex) {
Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
}

最佳答案

摆脱 while 循环,因为每次迭代时都会重新设置 JTable 的表模型,删除先前迭代中添加的任何信息。相反,简单地使用 ResultSet 和 DbUtils.resultSetToTableModel 来设置模型一次似乎更有意义。请注意,我从未使用过此实用程序,但它似乎必须以这种方式工作。例如,

int id=0,quantite=0;
double prix=0.0;
ResultSet rs=null;
String sql = "Select id,prix,quantite from commande where quantite="+Integer.parseInt(tfRecherche.getText());
CommandeDao commande = new CommandeDao();
Statement st = commande.getSt();
try {
rs= st.executeQuery(sql);

// add this
tbAffichage.setModel(DbUtils.resultSetToTableModel(rs));

// and get rid of this...
// while(rs.next()){
// ....
// }

} catch (SQLException ex) {
Logger.getLogger(UI.class.getName()).log(Level.SEVERE, null, ex);
} finally {
// close your connection here if not null, or use try-with resources
}

关于java - 跳过结果集中的第一行到 JTABLE 显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36022459/

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