当前位置: 代码迷 >> Java相关 >> 对于关闭连接等资源的有关问题
  详细解决方案

对于关闭连接等资源的有关问题

热度:1368   发布时间:2013-02-25 21:47:32.0
对于关闭连接等资源的问题
Java code
public ResultSet getRset(String sqlstr){     try    {        ct=this.getConn();        pm=ct.prepareStatement(sqlstr);        rs=pm.executeQuery();        closeall();    }   catch(Exception e)       {e.printStackTrace();}  finally   {   return rs;   }}public void closeall(){ try{  if(this.rs!=null){rs.close();this.rs=null;}  if(this.pm!=null){pm.close();this.pm=null;}  if(this.ct!=null){ct.close();this.ct=null;}}catch(Exception e){e.printStackTrace();}} 


这里return rs 前面已经 调用了函数 closeall(),还能确保这个rs有效吗?

------解决方案--------------------------------------------------------
肯定不行撒,你都已经关闭了ResultSet那么返回就已经没有数据了
------解决方案--------------------------------------------------------
最好把closeall中的code放到finally中。
------解决方案--------------------------------------------------------
关闭了就取不了数据了,一般是查出来,构成一个对象,放到list里,然后返回这个list
  相关解决方案