我正在从具有两个不同查询的两个表中选择数据。我正在使用一个连接和一个结果集。填充了两个表,但是第二个查询的resultet.next()返回false,尽管它必须为true。
我还尝试使用两个不同的PreparedStatement和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();
因此,由于存在从查询sql2重试的数据,因此程序应跳入第二个while循环。你有什么建议吗?谢谢!
答案 0 :(得分:0)
在关闭Prepared语句之前尝试关闭结果集。
此外,如果遇到异常,最好使用try / catch来清理内容。参见Must JDBC Resultsets and Statements be closed separately although the Connection is closed afterwards?