自己实现一个连接池 关键是要控制连接的关闭和读取
步骤1, 实现 Connection 接口 并覆盖里面的close() 方法.
package sky.sql;
import java.sql.*;
import java.util.*;
public class MyConn implements Connection ...{
sky.sql.ConnectionPool pool;
Connection con;
public MyConn(Connection con , ConnectionPool pool) ...{
this.con = con;
this.pool = pool;
}
public int getHoldability() throws SQLException ...{
return 0;
}
public int getTransactionIsolation() throws SQLException ...{
return 0;
}
public void clearWarnings() throws SQLException ...{
}
public void close() throws SQLException ...{
pool.put(this);
}
public void commit() throws SQLException ...{
con.commit();
}
public void rollback() throws SQLException ...{
con.rollback();
}
public boolean getAutoCommit() throws SQLException ...{
return con.getAutoCommit();
}
public boolean isClosed() throws SQLException ...{
return con.isClosed();
}
public boolean isReadOnly() throws SQLException ...{
return false;
}
public void setHoldability(int holdability) throws SQLException ...{
}
public void setTransactionIsolation(int level) throws SQLException ...{
}
public void setAutoCommit(boolean autoCommit) throws SQLException ...{
con.setAutoCommit(autoCommit);
}
public void setReadOnly(boolean readOnly) throws SQLException ...{
}
public String getCatalog() throws SQLException ...{
return "";
}
public void setCatalog(String catalog) throws SQLException ...{
}
public DatabaseMetaData getMetaData() throws SQLException ...{
return con.getMetaData();
}
public SQLWarning getWarnings() throws SQLException ...{
return null;
}
public Savepoint setSavepoint() throws SQLException ...{
return con.setSavepoint();
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException ...{
}
public void rollback(Savepoint savepoint) throws SQLException ...{
con.rollback(savepoint);
}
public Statement createStatement() throws SQLException ...{
return con.createStatement();
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency) throws
SQLException ...{
return con.createStatement(resultSetType,resultSetConcurrency);
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException ...{
return con.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
}
public Map getTypeMap() throws SQLException ...{
return null;
}
public void setTypeMap(Map map) throws SQLException ...{
}
public String nativeSQL(String sql) throws SQLException ...{
return "";
}
public CallableStatement prepareCall(String sql) throws SQLException ...{
return con.prepareCall(sql);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException ...{
return con.prepareCall(sql,resultSetType,resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException ...{
return con.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql) throws SQLException ...{
return con.prepareStatement(sql);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException ...{
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType,
import java.sql.*;
import java.util.*;
public class MyConn implements Connection ...{
sky.sql.ConnectionPool pool;
Connection con;
public MyConn(Connection con , ConnectionPool pool) ...{
this.con = con;
this.pool = pool;
}
public int getHoldability() throws SQLException ...{
return 0;
}
public int getTransactionIsolation() throws SQLException ...{
return 0;
}
public void clearWarnings() throws SQLException ...{
}
public void close() throws SQLException ...{
pool.put(this);
}
public void commit() throws SQLException ...{
con.commit();
}
public void rollback() throws SQLException ...{
con.rollback();
}
public boolean getAutoCommit() throws SQLException ...{
return con.getAutoCommit();
}
public boolean isClosed() throws SQLException ...{
return con.isClosed();
}
public boolean isReadOnly() throws SQLException ...{
return false;
}
public void setHoldability(int holdability) throws SQLException ...{
}
public void setTransactionIsolation(int level) throws SQLException ...{
}
public void setAutoCommit(boolean autoCommit) throws SQLException ...{
con.setAutoCommit(autoCommit);
}
public void setReadOnly(boolean readOnly) throws SQLException ...{
}
public String getCatalog() throws SQLException ...{
return "";
}
public void setCatalog(String catalog) throws SQLException ...{
}
public DatabaseMetaData getMetaData() throws SQLException ...{
return con.getMetaData();
}
public SQLWarning getWarnings() throws SQLException ...{
return null;
}
public Savepoint setSavepoint() throws SQLException ...{
return con.setSavepoint();
}
public void releaseSavepoint(Savepoint savepoint) throws SQLException ...{
}
public void rollback(Savepoint savepoint) throws SQLException ...{
con.rollback(savepoint);
}
public Statement createStatement() throws SQLException ...{
return con.createStatement();
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency) throws
SQLException ...{
return con.createStatement(resultSetType,resultSetConcurrency);
}
public Statement createStatement(int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException ...{
return con.createStatement(resultSetType,resultSetConcurrency,resultSetHoldability);
}
public Map getTypeMap() throws SQLException ...{
return null;
}
public void setTypeMap(Map map) throws SQLException ...{
}
public String nativeSQL(String sql) throws SQLException ...{
return "";
}
public CallableStatement prepareCall(String sql) throws SQLException ...{
return con.prepareCall(sql);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency) throws
SQLException ...{
return con.prepareCall(sql,resultSetType,resultSetConcurrency);
}
public CallableStatement prepareCall(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws
SQLException ...{
return con.prepareCall(sql,resultSetType,resultSetConcurrency,resultSetHoldability);
}
public PreparedStatement prepareStatement(String sql) throws SQLException ...{
return con.prepareStatement(sql);
}
public PreparedStatement prepareStatement(String sql, int autoGeneratedKeys) throws
SQLException ...{
return null;
}
public PreparedStatement prepareStatement(String sql, int resultSetType,