看来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
。有什么解决办法吗?
Use CachedRowSet
用于在断开连接后保留信息
Connection con = ...
ResultSet rs = ...
CachedRowSet rowset = new CachedRowSetImpl();
rowset.populate(rs);
con.close()
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)