public void close() throws SQLException {
array.add(this);
}
}
//自定义数据库连接池(实现DataSource接口)
private ArrayList<Connection> array = new ArrayList<>();
public MyDataSource() {
// TODO Auto-generated constructor stub
for (int i = 0; i < 10; i++) {
Connection conn = DBCPUtlist.getConnection();
Myconnection my = new Myconnection(conn, array);
array.add(my);
}
}
@Override
public Connection getConnection() throws SQLException {
// TODO Auto-generated method stub
return array.remove(0);
}
...
}
//然后在定义test测试类测试
public class Test {
public static void main(String[] args) {
MyDataSource mdy = new MyDataSource();
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
try {
conn = mdy.getConnection();
stat = conn.createStatement();
//执行SQL语句, 获取结果集
String sql = "select * from user;";
rs = stat.executeQuery(sql);
//操作结果集
while(rs.next()) {
System.out.println(rs.getInt("id") + "..." + rs.getString("name")+ "..." +
rs.getString("pwd") );
}
} catch (Exception e) {
e.printStackTrace();
} finally {
DBCPUtlist.remove(conn, stat, rs);
}
}
}