首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java相关 >

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

2012-04-03 
对于关闭连接等资源的问题Java codepublic ResultSet getRset(String sqlstr){try{ctthis.getConn()pmc

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

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

热点排行