我写了一个类 封装了MySQL数据库的连接方法,一个是用类方法实现,一个是用对象方法实现,如下:
public static Connection getConn() throws Exception{
Connection conn=null;
try{
Class.forName(strDataDriverName);
} catch (ClassNotFoundException e) {
// TODO: handle exception
throw new Exception(e);
}
try {
conn=DriverManager.getConnection(strDataURL,strDataUserName,strDataPWD);
} catch (SQLException e) {
// TODO: handle exception
throw new Exception(e);
}
return conn;
}
//
public Connection getConnection() throws Exception {
Connection conn=null;
try{
Class.forName(strDataDriverName);
} catch (ClassNotFoundException e) {
// TODO: handle exception
throw new Exception(e);
}
try {
conn=DriverManager.getConnection(strDataURL,strDataUserName,strDataPWD);
} catch (SQLException e) {
// TODO: handle exception
throw new Exception(e);
}
return conn;
}
这两个方法都没有关闭conn, 因为要返回conn, 我想问下 如果在外部声明一个 conn=类.getConn 后 然后关闭conn 是不是接可以了也就同样的关闭了类.getConn中的conn,主要是 这两个conn对象是不是一个?
如果用下面的对象.getConnection, 疑惑同上!!