public class DbConn {
Connection conn = null;//类实例变量-> 因为java的线程机制这样写可能造成冲突。
Statement stmt = null;//类实例变量-> 因为java的线程机制这样写可能造成冲突。
ResultSet rs = null;//类实例变量-> 因为java的线程机制这样写可能造成冲突。
public ResultSet Execute(String sql) {
try {
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
return rs;
} catch (Exception ex) {
ex.printStackTrace();
}
return rs;
}
public void Close() {
try {
if (rs != null) {
rs.close();
rs = null;
}
if (stmt != null) {
stmt.close();
stmt = null;
}
if (conn != null) {
conn.close();
conn = null;
}
} catch (Exception ex) {
;
}
}
}
//改造如下:
public class DbConn1 {
public static Connection getConn(){
Connection conn = null;
//conn连接代码
return conn;
}
/**
* 如果这样,在其他的bean中或jsp中调用的话,怎样关闭局部变量conn和stmt呢?
* */
public ResultSet Execute(String sql) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
//conn连接代码
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_UPDATABLE);
rs = stmt.executeQuery(sql);
return rs;
} catch (Exception ex) {
ex.printStackTrace();
}
return rs;
}
}
------解决方案--------------------
不错