我正在使用两个不同的查询从两个表中选择数据。我正在使用一个连接和一个结果集。两个表都已填充,但第二个查询的 resultset.next() 返回 false,尽管它必须为 true。
我还尝试使用两个不同的PreparedStatements 和 Connections,但这些都不适合我。
DataSource ds = null;
Connection c = null;
PreparedStatement ps = null;
String sql = "SELECT * FROM TABLE1"
String sql2 = "SELECT * FROM TABLE2"
ds = // My datasource
c = ds.getConnection();
ps = c.prepareStatement(sql);
ResultSet resultSet = ps.executeQuery();
while (resultSet.next()) {
// do smth
// works
}
ps.close();
ps = c.prepareStatement(sql2);
resultSet = ps.executeQuery();
while (resultSet.next()) {
// do somth
// does not work although TABLE2 is populated
}
ps.close();
因此,程序应该跳转到第二个 while 循环,因为查询 sql2 有数据返回。你有什么建议吗?谢谢!
最佳答案
在关闭准备好的语句之前尝试关闭结果集。
此外,使用 try/catch 以便在出现异常时清理内容是非常好的做法。请参阅Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards?
关于java - 一个 ResultSet/ResultSet.next() 中的多个语句返回 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56770170/