日期:2014-05-20  浏览次数:20585 次

对于关闭连接等资源的问题
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