此代码仅从数据库中检索 1 条记录。
如何使用下一步按钮从数据库检索其余记录
public void Next()
{
Connection con=null;
ResultSet rs=null;
try {
con=DBConnection();
Statement stmt=con.createStatement();
rs= stmt.executeQuery("select * from info where id=1");
while(rs.next())
{
t1.setText(rs.getString("Name"));
t2.setText(rs.getString("Branch"));
}
} catch (Exception e) {
// TODO: handle exception
}finally{
try {
rs.close();
} catch (SQLException err) {
JOptionPane.showMessageDialog(btnnext, err.getMessage());
// TODO: handle exception
}
}
//return rs;
}
最佳答案
首先分两步执行此操作:
在 next() 方法之外创建数据库连接,除非您无法获取其他记录:
Connection con=null;
ResultSet rs=null;
try {
con=DBConnection();
rs = con.createStatement().executeQuery("select * from info where id=1");
}
其次,迭代查询:
public boolean Next()
{
if(!rs.next()) { //if no next then we should deactivate the next button
return false;
} else {
t1.setText(rs.getString("Name"));
t2.setText(rs.getString("Branch"));
return true;
}
}
另请确保以下查询:
select * from info where id=1
将返回超过 1 行,您可以尝试使用没有 where 并返回更多行的查询。
关于java - 无法从记录集中检索多个结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17520980/