Java 使用“向前”和“向后”导航结果集
在java中,如果我们想使用两个按钮向前和向后浏览通过执行查询得到的结果集。我们可以用什么方式完成这项任务Java 使用“向前”和“向后”导航结果集,java,sql,Java,Sql,在java中,如果我们想使用两个按钮向前和向后浏览通过执行查询得到的结果集。我们可以用什么方式完成这项任务 private void nextActionPerformed(java.awt.event.ActionEvent evt) { if(evt.getSource()==bt_previous){ DBUtil util = new DBUtil();
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());
}
}
}
您需要对结果进行分页。 总之,使用结果集并没有简单的任务,也没有内置的特性。 您有以下选项:
page
,item\u id
。将其命名为history\u table
。然后读取前50个对象(假设您希望一次显示50行)。这是第一页。在历史记录表中插入唯一标识符以及页码。
当需要返回时,从上一页读取唯一ID。前进时,执行selectselect * from my_table a where not exists (select 1 from history_table h where a.item_id = h.item_id)
您可以为每个需要的页面(甚至是第一个页面)执行此命令
在数据库中插入以前读取的元素时应使用。创建实体类
详细信息
:
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));
}
List allDetails=new LinkedList();
while(rs.next()){
字符串a=rs.getString(“重量”);
字符串b=rs.getString(“注释状态”);
字符串c=rs.getString(“dm_状态”);
添加(新详图(a、b、c));
}
然后,您可以使用LinkedList
支持的任何方式浏览allDetails
。或者,如果LinkedList不符合你的需要,请考虑使用另一个集合。