自己实现一个连接池 关键是要控制连接的关闭和读取
步骤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,
int resultSetConcurrency) throws

SQLException ...{
return con.prepareStatement(sql,resultSetType,resultSetConcurrency);
}

public PreparedStatement prepareStatement(String sql, int resultSetType,
int resultSetConcurrency,
int resultSetHoldability) throws

SQLException ...{
return null;
}

public PreparedStatement prepareStatement(String sql, int[] columnIndexes) throws

SQLException ...{
return null;
}


public Savepoint setSavepoint(String name) throws SQLException ...{
return null;
}

public PreparedStatement prepareStatement(String sql, String[] columnNames) throws

SQLException ...{
return null;
}

public void closeTrue()...{

try ...{
con.close();

} catch (SQLException ex) ...{
}
}

}
2.定义获得连接的工厂
package
sky.sql;
import
java.util.ArrayList;
import
java.sql.
*
;


public
class
ConnectionPool
...
{
static ConnectionPool myPool;
java.util.ArrayList al = new ArrayList();
//设置连接池中连接的个数
int size =5;

private ConnectionPool() ...{

try ...{
Class.forName("com.microsoft.jdbc.sqlserver.SQLServerDriver");

for (int i = 0; i <size; i++) ...{
Connection con = java.sql.DriverManager.getConnection(
"jdbc:microsoft:sqlserver://localhost:1433;databasename=startSky",
"sa", "");
MyConn conn = new MyConn(con,this);
al.add(conn);
}

} catch (SQLException ex) ...{
ex.printStackTrace();

} catch (ClassNotFoundException ex) ...{
ex.printStackTrace();
}

}

public static ConnectionPool getPool()...{

if(myPool == null)...{
myPool = new ConnectionPool();
return myPool;


}else...{
return myPool;
}
}


public Connection getConn()...{
return (Connection) al.remove(0);
}

public void put(Connection con)...{
al.add(con);
}

public void closeAll()...{

for(int i=0;i<al.size();i++)...{
MyConn con = (MyConn) al.get(i);
con.closeTrue();
}
}

}