public class C3P0Demo2 {
public static void main(String[] args) {
//创建C3P0的数据库连接池
//ComboPooledDataSource cpds = new ComboPooledDataSource();
//首先创建C3P0的数据库连接池构造方法
//ComboPooledDataSource(configName)
//configName表示新增的名字
ComboPooledDataSource cpds = new ComboPooledDataSource(“day04”);
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
//从连接池中获取连接
conn = cpds.getConnection();
//获得发送SQL的对象
String sql = “select * from user”;
stmt = conn.prepareStatement(sql);
//执行sql获得结果
rs = stmt.executeQuery();
while (rs.next()) {
int id = rs.getInt(“id”);
String username = rs.getString(“username”);
String password = rs.getString(“password”);
System.out.println(“C3P0:::::” + id + “>>>” + username + “>>>” + password);
}
}catch (Exception e){
e.printStackTrace();
}finally {
JDBCUtils.release(conn,stmt,rs);
}
}
}
C3P0数据库连接池的运用
最新推荐文章于 2021-11-17 17:45:36 发布