【发布时间】:2010-09-24 04:47:20
【问题描述】:
考虑代码:
PreparedStatement ps = null;
ResultSet rs = null;
try {
ps = conn.createStatement(myQueryString);
rs = ps.executeQuery();
// process the results...
} catch (java.sql.SQLException e) {
log.error("an error!", e);
throw new MyAppException("I'm sorry. Your query did not work.");
} finally {
ps.close();
rs.close();
}
上面没有编译,因为PreparedStatement.close() 和ResultSet.close() 都抛出了java.sql.SQLException。那么我应该在 finally 子句中添加一个 try/catch 块吗?或者将 close 语句移到 try 子句中?或者只是不打扰打电话关闭?
【问题讨论】:
标签: java jdbc resource-management