日期:2014-05-18  浏览次数:20606 次

这个数据库连接有办法关闭吗?
class     DBConn{
        private   Connection   conn=null;
        public   DBConn(){
                try{
                        Class.forName(DBDRIVER);//   加载驱动程序
                        this.conn=DriverManager.getConnection(DBURL,DBUSER,DBPASSWORD);//   连接数据库
                }catch(ClassNotFoundException   e){
                        e.printStackTrace();
                }catch(SQLException   e){
                        e.printStackTrace();
                }
        }
        public   Connection   getConnection(){
                return   this.conn;
        }
        public   void   close(){
                try{
                        this.conn.close();
                }catch(SQLException   e){
                        e.printStackTrace();
                }
        }
}

class   Test{
private   Connection   conn=null;
DBConn   db=new   DBConn();
public   Test(){
conn=db.getConnection();
}
public   TestVO   queryById(){
}
}

在某个类中使用
TestVO   t=new   Test().queryById();

用Test()产生的连接有办法关闭吗?

------解决方案--------------------
把事务写在方法里

public TestV0 querById()
{
//取得链接

//查询数据

//关闭链接
}