读书人

这个数据库连接有办法关闭吗?解决方案

发布时间: 2012-03-02 14:40:28 作者: rapoo

这个数据库连接有办法关闭吗?
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()
{
//取得链接

//查询数据

//关闭链接
}

读书人网 >Java Web开发

热点推荐