当我ResultSet
关闭Connection
. 但是我想返回ResultSet
并在另一种方法中使用它,然后我不知道在哪里关闭Connection
and PreparedStatement
。
public ResultSet executeQuery(String sql, String[] getValue)
{
Connection conn = null;
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
conn = getConn();
pstmt = conn.prepareStatement(sql);
if (getValue != null)
{
for (int i = 0; i < getValue.length; i++)
{
pstmt.setString(i + 1, getValue[i]);
}
}
rs = pstmt.executeQuery();
} catch (Exception e)
{
e.printStackTrace();
closeAll(conn, pstmt, rs);
}
return rs;
}
我已经closeAll(conn, pstmt, null);
进入了 catch 块,因为我发现如果我把它放在 finally 块中,我会rs
在它返回之前立即丢失我的。现在当我想关闭 时rs
,我无法关闭conn
and pstmt
。有什么解决办法吗?