我需要做一些操作,我有這個代碼:
ResultSet rsSel = null;
// preparated statemen
PreparedStatement stmtSel = null;
// preparated statemen
PreparedStatement stmtUpd = null;
try {
//create query yyyyyyyy
String query = SQLStatements.getStatement("queryExample");
//create PreparedStatement
stmtSel = (PreparedStatement) conn.prepareStatement(query);
int index = 1;
// WHERE:
stmtSel.setInt(index , prog);
// SELECT:
rsSel = stmtSel.executeQuery(); //the problem is here
rsSel.next();
//close finally
rsSel.close()
在我的最后我做:
finally{
close(stmtSel);
close(rsSel);
}
我不知道為什么程式執行時rsSel = stmtSel.executeQuery();它給了我:
Closing a result set you left open! Please close it yourself.: java.lang.Throwable: STACKTRACE
任何人都可以幫助我嗎?
uj5u.com熱心網友回復:
該錯誤是由范圍太寬引起的,可能是隱藏的例外或控制流中的某些怪癖。最好是使用 try-with-resources,它也在回傳/中斷/例外時關閉。那么你會有:
String query = SQLStatements.getStatement("queryExample");
try (PreparedStatement stmtSel = conn.prepareStatement(query)) {
int index = 1;
stmtSel.setInt(index , prog);
...
try (ResultSet rsSel = stmtSel.executeQuery()) {
if (rsSel.next()) {
...
}
} // Closes rsSel
} // Closes stmtSel
假設您匯入java.sql.PreparedStatement.
順便一提:
Java 中的縮進是根據約定設定為 4 的介紹。人們想要“不那么深的陳述句嵌套”。我現在在 Delphi Pascal 專案中縮進了 2。java中的約定得到了很好的維護。
uj5u.com熱心網友回復:
先關閉 rssel 再關閉 stmtsel
try{
......
} finally{
close(rsSel);
close(stmtSel);
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/361897.html
下一篇:洗掉ODI中I$表中的重復項
