我正在创建JDBC Statement
s和ResultSet
s。如果抛出异常,确保对象关闭
Findbugs正确地指出,如果抛出异常,我不会close
这些。
所以现在我有:
Statement stmt = null;
ResultSet res = null;
try {
stmt = ...
res = stmt.executeQuery(...);
...
} finally {
try {
if(res != null)
res.close(); // <-- can throw SQLException
} finally {
if(stmt != null)
stmt.close();
}
}
(只有我有相当多的结果集,并准备报表等开放...所以我的finally
小号嵌套是相当深)
有有以更好的方式确保大量结果集被关闭?我们认为这是一个非常好的设计决策,因为JDBC中的所有close
方法都可能抛出SQLException
,这使事情变得不必要的复杂化,在我看来。)
为什么你在'finally'中有'try-finally'?应该有一些“catch”......对吧? – Bill 2013-05-13 12:51:33
如何将所有语句和结果集存储在列表中并遍历列表? – reporter 2013-05-13 12:52:08
@Bill:如果第一个'close'抛出异常。 – 2013-05-13 12:53:30