当前位置: 代码迷 >> Eclipse >> 返回结果集有关问题,初学者求解
  详细解决方案

返回结果集有关问题,初学者求解

热度:45   发布时间:2016-04-23 12:18:01.0
返回结果集问题,菜鸟求解
调用一下函数,但是会显示结果集已关闭,去掉this.closeAll(conn, pstmt, null);后可以用,但是没关掉Connection和PreparedStatement,求大神一个好的方法处理结果集的返回。
  public ResultSet SelectSQL(String sql)
{
conn=this.getConnection();
ResultSet rs=null;
PreparedStatement pstmt=null;
try {
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
this.closeAll(conn, pstmt, null);
}
return rs;
}

------解决方案--------------------
你都已经在finally中this.closeAll(conn, pstmt, null);
连接被关闭了,当然结果集也会被关闭掉。


一般建议的做法是,不要直接返回ResultSet,而是将其转换为 List 列表,这样就不用担心结果集被关闭了,而且也不会暴露了数据库对象;这是推荐的标准的做法。


如果你非要用ResultSet作为返回,可以用CachedRowSet,它会自动把查询结果集全部加载到内存中,就不怕连接被关闭了;具体可以Google下。
  相关解决方案