1.参看CP30文档quickStart
如下具体创建步骤
1 public DataSource getDs() throws Exception { 2 //创建连接池对象 3 ComboPooledDataSource cpds = new ComboPooledDataSource(); 4 cpds.setDriverClass("com.mysql.jdbc.Driver"); 5 cpds.setJdbcUrl("jdbc:mysql://localhost:3306/jdbcdemo"); 6 cpds.setUser("root"); 7 cpds.setPassword("4230"); 8 return cpds; 9 }
2.调用上述方法创造的DataSource连接池对象进行查询操作
1 @Test 2 public void testQuery() throws Exception { 3 Connection connec = getDs().getConnection(); 4 String sql = "SELECT * FROM student"; 5 PreparedStatement ps = connec.prepareStatement(sql); 6 ResultSet rSet = ps.executeQuery(); 7 while(rSet.next()){ 8 System.out.println(rSet.getLong(1) + "-" + rSet.getString(2) + "-" + rSet.getInt(3)); 9 } 10 DBCPUtil.close(rSet, ps, connec); 11 }
时间: 2024-11-06 18:11:36