JDBC 通过Wrapper接口释放连接资源(关闭连接的新方法)


虽然没有大用,但用着方便,有时间的童鞋可以看一下,第一段代码是模拟测试,第二段代码是例子
package ace.wisdom;

import static java.lang.System.out;
import static ace.wisdom.DBUtil.releaseResource;

public class Test {

    public static void main(String[] args) {

        /* 先 new 好对象 */

        Connection connection = new ConnectionConcrete();
        PreparedStatement preparedStatement = new PreparedStatementConcrete();
        Statement statement = new StatementConcrete();
        CallableStatement callableStatement= new CallableStatementConcrete();
        ResultSet resultSet = new ResultSetConcrete();
        /* 三种顺序测试了关闭资源 */

        releaseResource(resultSet);
        releaseResource(statement);
        releaseResource(preparedStatement);
        releaseResource(callableStatement);
        releaseResource(connection);

        out.println("========== separating character ==========");

        releaseResource(connection);
        releaseResource(callableStatement);
        releaseResource(preparedStatement);
        releaseResource(statement);
        releaseResource(resultSet);

        out.println("========== separating character ==========");

        releaseResource(statement);
        releaseResource(null);
        releaseResource(null);
        releaseResource(callableStatement);
        releaseResource(null);
    }
}

abstract class DBUtil {
    public static void releaseResource(Wrapper instance) {
        if (instance == null) {
            // Ignored..
            out.println("0  Instance is null");
            return ;
        } 
        if (instance instanceof Connection) {
            out.print("1  ");
            ((Connection)instance).close();
            return ;
        }
        if (instance instanceof Statement) {
            out.print("2  ");
            ((Statement)instance).close();
            return ;
        }
        if (instance instanceof ResultSet) {
            out.print("3  ");
            ((ResultSet)instance).close();
            return ;
        }
        out.println("如果执行到这条语句,实例释放失败!");
    }
}

interface Wrapper {
    
}
interface Connection extends Wrapper {
    void close();
}
interface Statement extends Wrapper {
    void close();
}
interface ResultSet extends Wrapper {
    void close();
}
interface PreparedStatement extends Statement {
    
}
interface CallableStatement extends PreparedStatement {
    
}

class ConnectionConcrete implements Connection {
    @Override
    public void close() {
        out.println("Connection is closed");
    }
}
class StatementConcrete implements Statement {
    @Override
    public void close() {
        out.println("Statement is closed");
    }
}
class PreparedStatementConcrete implements PreparedStatement {
    @Override
    public void close() {
        out.println("PreparedStatement is closed");
    }
}
class CallableStatementConcrete implements CallableStatement {
    @Override
    public void close() {
        out.println("CallableStatement is closed");
    }
}
class ResultSetConcrete implements ResultSet {
    @Override
    public void close() {
        out.println("ResultSet is closed");
    }
}

输出结果:

3 ResultSet is closed
2 Statement is closed
2 PreparedStatement is closed
2 CallableStatement is closed
1 Connection is closed
========== separating character ==========
1 Connection is closed
2 CallableStatement is closed
2 PreparedStatement is closed
2 Statement is closed
3 ResultSet is closed
========== separating character ==========
2 Statement is closed
0 Instance is null
0 Instance is null
2 CallableStatement is closed
0 Instance is null


例:

package ace.wisdom;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Wrapper;

public abstract class BaseDAO {

    static DataSource _source;

    static {AceTest.
        _source = new ConnectionPool();
    }

    public Connection getConnectionFromDriver() {
        try {
            return _source.getConnectionFromDriver();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 只要这一个方法,想关什么就关什么,非常好用,而且就几行代码,一个一个关就行
     */
    public void releaseResource(Wrapper res) {
        try {
            if (res instanceof Connection && !((Connection) res).isClosed()) {
                ((Connection) res).close();
            } else if (res instanceof Statement) {
                ((Statement) res).close();
            } else if (res instanceof ResultSet) {
                ((ResultSet) res).close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            res = null;
        }
    }

    /**
     * 这是一起关闭的,java.sql.Wrapper.isWrapperFor()
     */
    public void releaseResources(Connection connection, Statement stmt, ResultSet rs) {
        releaseResource(rs);
        releaseResource(stmt);
        releaseResource(connection);
    }

    public PreparedStatement setValues(PreparedStatement pstmt, Object[] params) {
        try {
            for (int i = 0; i < params.length; ++i) {
                pstmt.setObject(i + 1, params[i]);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return pstmt;
    }
}

package ace.wisdom;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class AceTest extends BaseDAO {

    static final String USERNAME = "USERNAME";
    static final String PASSWORD = "PASSWORD";

    public static void main(String[] args) {
        
        AceTest foo = new AceTest();
        
        foo.test();
    }
    
    void test() {
        long ID = 1;
        Connection connection = this.getConnectionFromDriver();
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        try {
            String sqlQuery = " SELECT USERNAME, PASSWORD FROM USER_INFO WHERE ID = ?";
            Object[] x = { 
                    ID
            };
            pstmt = connection.prepareStatement(sqlQuery);
            rs    = setValues(pstmt, x).executeQuery();
            while (rs.next()) {
                System.out.print(rs.getString(USERNAME));
                System.out.print("\t");
                System.out.println(rs.getString(PASSWORD));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //releaseResource(rs);
            //releaseResource(pstmt);
            //releaseResource(connection);

            releaseResources(connection, pstmt, rs);
        }
    }
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值