/**
- 手动实现连接池
- @author IBM
*/
public class test01 {
//容器-池子
private static List<Connection> rq = new ArrayList<>();
//默认池子初始化的连接数量 5
private static int pool_size = 5;
//连接池初始化
static {
for (int i = 0; i < pool_size; i++) {
Connection connection = JDBCTools.getConnection();
rq.add(connection);
System.out.println("存放-池:" +connection + "当前池子中连接数:"+rq.size());
}
}
//从池子中获取连接
public static Connection getConnectionByPool() {
//从池子读取当前连接并且将当前连接从池子中移除
Connection connection = rq.remove(0);
System.out.println("取出-池:"+connection + "当前池子中连接数:"+rq.size() );
return connection;
}
//创建一个归还连接的方法
public static void clearConnection(Connection c , PreparedStatement ps, ResultSet rs) {
if(null != rs) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != ps) {
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(null != c) {
//将这个连接存到池子中
rq.add(c);
System.out.println("归还-池:"+c + "当前池子中连接数:"+rq.size() );
}
}
}