求指教JAVA程序中连接数据库访问后彻底关闭连接问题 5
packagefurnace.dbconnect;importjava.sql.*;publicclassDBConnect{publicConnectionconn=n...
package furnace.dbconnect;
import java.sql.*;
public class DBConnect {
public Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private DBConnectionManager dcm=null;
public CallableStatement cstm = null;
void init() {
dcm = DBConnectionManager.getInstance();
conn = dcm.getConnection("mssql");
}
/**
* 构造数据库的连接和访问类
*/
public DBConnect() throws Exception {
init();
stmt = conn.createStatement();
}
public DBConnect(int resultSetType, int resultSetConcurrency)
throws Exception {
init();
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* 构造数据库的连接和访问类
* 预编译SQL语句
* @param sql SQL语句
*/
public DBConnect(String sql) throws Exception {
init();
this.prepareStatement(sql);
}
public DBConnect(String sql, int resultSetType, int resultSetConcurrency)
throws Exception {
init();
this.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
.................................
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 关闭连接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
if (cstm != null) {
cstm.close();
cstm = null;
}
if (conn!=null)
{
dcm.freeConnection("mssql",conn);
}
}
}
最后的CONN没有彻底关闭,请大神指教怎么实现CONN彻底关闭 展开
import java.sql.*;
public class DBConnect {
public Connection conn = null;
private Statement stmt = null;
private PreparedStatement prepstmt = null;
private DBConnectionManager dcm=null;
public CallableStatement cstm = null;
void init() {
dcm = DBConnectionManager.getInstance();
conn = dcm.getConnection("mssql");
}
/**
* 构造数据库的连接和访问类
*/
public DBConnect() throws Exception {
init();
stmt = conn.createStatement();
}
public DBConnect(int resultSetType, int resultSetConcurrency)
throws Exception {
init();
stmt = conn.createStatement(resultSetType, resultSetConcurrency);
}
/**
* 构造数据库的连接和访问类
* 预编译SQL语句
* @param sql SQL语句
*/
public DBConnect(String sql) throws Exception {
init();
this.prepareStatement(sql);
}
public DBConnect(String sql, int resultSetType, int resultSetConcurrency)
throws Exception {
init();
this.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
/**
* 返回连接
* @return Connection 连接
*/
public Connection getConnection() {
return conn;
}
/**
* PreparedStatement
* @return sql 预设SQL语句
*/
public void prepareStatement(String sql) throws SQLException {
prepstmt = conn.prepareStatement(sql);
}
public void prepareStatement(String sql, int resultSetType, int resultSetConcurrency)
throws SQLException {
prepstmt = conn.prepareStatement(sql, resultSetType, resultSetConcurrency);
}
.................................
/**
* 返回预设状态
*/
public PreparedStatement getPreparedStatement() {
return prepstmt;
}
/**
* 返回状态
* @return Statement 状态
*/
public Statement getStatement() {
return stmt;
}
/**
* 执行SQL语句返回字段集
* @param sql SQL语句
* @return ResultSet 字段集
*/
public ResultSet executeQuery(String sql) throws SQLException {
if (stmt != null) {
return stmt.executeQuery(sql);
}
else return null;
}
public ResultSet executeQuery() throws SQLException {
if (prepstmt != null) {
return prepstmt.executeQuery();
}
else return null;
}
/**
* 执行SQL语句
* @param sql SQL语句
*/
public void executeUpdate(String sql) throws SQLException {
if (stmt != null)
stmt.executeUpdate(sql);
}
public void executeUpdate() throws SQLException {
if (prepstmt != null)
prepstmt.executeUpdate();
}
/**
* 关闭连接
*/
public void close() throws Exception {
if (stmt != null) {
stmt.close();
stmt = null;
}
if (prepstmt != null) {
prepstmt.close();
prepstmt = null;
}
if (cstm != null) {
cstm.close();
cstm = null;
}
if (conn!=null)
{
dcm.freeConnection("mssql",conn);
}
}
}
最后的CONN没有彻底关闭,请大神指教怎么实现CONN彻底关闭 展开
4个回答
展开全部
try{
}catch(){
}finnaly{
//关闭数据库连接
}
}catch(){
}finnaly{
//关闭数据库连接
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
java 异常处理 你可以看看 finally
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
问刘十九(白居易)
追问
刘十九(白居易),请问此乃何人?
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询