![](/img/trans.png)
[英]ResultSet is not null still resultset.next() returning false
[英]Multiple Statements in one ResultSet / ResultSet.next() returning false
我從帶有兩個不同查詢的兩個表中選擇數據。 我正在使用一個連接和一個結果集。 兩個表都已填充,但是第二個查詢的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循環。 你有什么建議嗎? 謝謝!
在關閉preparedstatement之前,請嘗試關閉結果集。
另外,如果遇到異常,使用try / catch進行清理也是一種很好的做法。 請參閱盡管之后連接已關閉,但是否必須分別關閉JDBC結果集和語句?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.