DBCP连接池的使用:
/*
* dbcp 连接池的使用 :
*
*/
public class DbcpTest {
@Test
public void test1(){
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 类加载器 读
InputStream in = DbcpTest.class.getClassLoader().getResourceAsStream("dbcp.properties");
Properties prop = new Properties();
prop.load(in);
DataSource ds = BasicDataSourceFactory.createDataSource(prop);
conn = ds.getConnection();
stmt = conn.prepareStatement("select * from account");
rs = stmt.executeQuery();
while(rs.next()){
String name = rs.getString("name");
String money = rs.getString("money");
System.out.println( " name : " + name +", money : " + money);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtils.release(rs, stmt, conn);
}
}
@Test
public void test2(){
BasicDataSource bds = new BasicDataSource();
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
try {
// 设置 数据库连接池的 一些 信息
bds.setDriverClassName("com.mysql.jdbc.Driver");
bds.setUrl("jdbc:mysql:///day13");
bds.setUsername("root");
bds.setPassword("erqiu");
conn = bds.getConnection();
stmt = conn.prepareStatement("select * from account");
rs= stmt.executeQuery();
while(rs.next()){
String name = rs.getString("name");
String money = rs.getString("money");
System.out.println( " name : " + name +", money : " + money);
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
JdbcUtils.release(rs, stmt, conn);
}
}
}
配置文件 :dbcp.properties
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql:///day13
username=root
password=erqiu