📄 connectionwrapper.java
字号:
package com.zhanghao.common.database;import java.sql.*;import java.util.Vector;import java.util.Map;import com.zhanghao.common.util.*;/** * <p>数据库连接包装类。 </p> * <p>Description: </p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: </p> * @author not attributable * @version 1.0 */class ConnectionWrapper implements Connection { private Vector freeConns; //用来存放数据库连接的集合,参见ConnectionPool类的freeConns属性 private Connection conn; //被包装的数据库连接 private long createdTime; //数据库连接建立的时间。用长整型数字代表时间 /** * 构造器。只供同一个包内的类使用。具体说来,只供ConnectionPool类使用 * @param conn 数据库连接 * @param freeConns 用来存放数据库连接的集合,参见ConnectionPool类的freeConns属性 * @throws SQLException */ ConnectionWrapper(Connection conn,Vector freeConns) throws SQLException { if(conn==null || freeConns==null){ throw new SQLException("ConnectionWrapper,建立数据库连接失败"); } this.conn = conn; this.freeConns = freeConns; createdTime = System.currentTimeMillis(); } /** * 私有造器。用一个新的包装器来包装数据库连接,连接建立时间按照原来的时间计算。 * 本构造器只用在这个类的close()方法里 * @param wrapper 数据库连接包装类 * @param freeConns 用来存放数据库连接的集合,参见ConnectionPool类的freeConns属性 * @throws SQLException */ private ConnectionWrapper(ConnectionWrapper wrapper) { this.conn = wrapper.conn; this.freeConns = wrapper.freeConns; this.createdTime = wrapper.createdTime; } /** * 取得被包装的物理连接。 * 这个方法只供ConnectionPool类调用 * @return 物理连接 */ Connection getPhysicalConnection(){ return conn; } /** * 取得本连接的建立时间。这个方法只供ConnectionPool类调用 * @return 建立时间 */ long getCreatedTime(){ return createdTime; } /** * 释放数据库连接 * @throws SQLException */ public void close() throws SQLException{ //本方法并不真正释放物理的数据库连接,只是把它返回数据库连接池 ConnectionWrapper newWrapper = new ConnectionWrapper(this); this.conn = null; //把指针置为空 //把物理的数据库连接用新的包装器包装后,放回到连接池队列的最后: freeConns.addElement(newWrapper); } public boolean isClosed() throws SQLException { if(conn==null) return true; return conn.isClosed(); } public Statement createStatement() throws SQLException { //checkClosed(); return conn.createStatement(); } public PreparedStatement prepareStatement(String sql) throws SQLException{ return conn.prepareStatement(sql); } public CallableStatement prepareCall(String sql) throws SQLException { return conn.prepareCall(sql); } public String nativeSQL(String sql) throws SQLException{ return conn.nativeSQL(sql); } public void setAutoCommit(boolean autoCommit) throws SQLException{ conn.setAutoCommit(autoCommit); } public boolean getAutoCommit() throws SQLException{ return conn.getAutoCommit(); } public void commit() throws SQLException { conn.commit(); } public void rollback() throws SQLException{ conn.rollback(); } public DatabaseMetaData getMetaData() throws SQLException{ return conn.getMetaData(); } public void setReadOnly(boolean readOnly) throws SQLException{ conn.setReadOnly(readOnly); } public boolean isReadOnly() throws SQLException{ return conn.isReadOnly(); } public void setCatalog(String catalog) throws SQLException{ conn.setCatalog(catalog); } public String getCatalog() throws SQLException{ return conn.getCatalog(); } public void setTransactionIsolation(int level) throws SQLException{ conn.setTransactionIsolation(level); } public int getTransactionIsolation() throws SQLException { return conn.getTransactionIsolation(); } public SQLWarning getWarnings() throws SQLException{ return conn.getWarnings(); } public void clearWarnings() throws SQLException{ conn.clearWarnings(); } public Statement createStatement(int resultSetType, int resultSetConcurrency) throws SQLException{ return conn.createStatement(resultSetType,resultSetConcurrency); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency) throws SQLException{ return conn.prepareStatement(sql,resultSetType,resultSetConcurrency); } public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency) throws SQLException{ return conn.prepareCall(sql,resultSetType,resultSetConcurrency); } public Map getTypeMap() throws SQLException{ return conn.getTypeMap(); } public void setTypeMap(Map map) throws SQLException{ conn.setTypeMap(map); } public void setHoldability(int holdability) throws SQLException{ conn.setHoldability(holdability); } public int getHoldability() throws SQLException{ return conn.getHoldability(); } public Savepoint setSavepoint() throws SQLException{ return conn.setSavepoint(); } public Savepoint setSavepoint(String name) throws SQLException { return conn.setSavepoint(name); } public void rollback(Savepoint savepoint) throws SQLException{ conn.rollback(savepoint); } public void releaseSavepoint(Savepoint savepoint) throws SQLException{ conn.releaseSavepoint(savepoint); } public Statement createStatement(int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{ return conn.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability); } public PreparedStatement prepareStatement(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{ return conn.prepareStatement(sql,resultSetType, resultSetConcurrency,resultSetHoldability); } public CallableStatement prepareCall(String sql, int resultSetType, int resultSetConcurrency, int resultSetHoldability) throws SQLException{ return conn.prepareCall(sql,resultSetType, resultSetConcurrency,resultSetHoldability); } public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws SQLException{ return conn.prepareStatement(sql,autoGeneratedKeys); } public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws SQLException{ return conn.prepareStatement(sql,columnIndexes); } public PreparedStatement prepareStatement(String sql, String[] columnNames) throws SQLException { return conn.prepareStatement(sql,columnNames); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -