gpt4 book ai didi

java - 使用向前和向后导航结果集

转载 作者:行者123 更新时间:2023-12-02 07:19:44 25 4
gpt4 key购买 nike

在java中,如果我们想要使用向前和向后两个按钮来导航我们通过执行查询获得的结果集。我们可以通过什么方式来完成这个任务

    private void nextActionPerformed(java.awt.event.ActionEvent evt) {                                            
if(evt.getSource()==bt_previous){
DBUtil util = new DBUtil();
try {
Connection con = util.getConnection();
PreparedStatement stmt = con.prepareStatement("SELECT * FROM DETAILS where id=?");
ResultSet rs;
String rm = id.getText().trim();
stmt.setLong(1, Long.parseLong(rm));
rs = stmt.executeQuery();
while(rs.next()){

String a = rs.getString("weight");
txtboxwgt.setText(a);
String b = rs.getString("note_state");
cbnotstat.setSelectedItem(b);
String c = rs.getString("dm_state");
cbdmnstat.setSelectedItem(c);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e.getMessage());
}
}


}

最佳答案

制作一个实体类详细信息:

public class Detail {
private String weight;
private String noteState;
private String dmState;

public Detail() {

}

public Detail(String weight, String noteState, String dmState) {
this.weight = weight;
this.noteState = noteState;
this.dmState = dmState;
}
// getters and setters below
}

在您的 nextActionPerformed 方法中,当您迭代结果集时,创建实体的新实例并将其添加到某个集合,即 LinkedList:

                List<Detail> allDetails = new LinkedList<Detail>();
while(rs.next()) {

String a = rs.getString("weight");
String b = rs.getString("note_state");
String c = rs.getString("dm_state");
allDetail.add(new Detail (a, b, c));
}

然后您可以以 LinkedList 支持的任何方式导航 allDetails。或者,如果 LinkedList 不能满足您的需求,请考虑使用其他集合。

关于java - 使用向前和向后导航结果集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14413364/

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