一开始先在内存中开辟一块空间(集合),一开始先往池子里放置多个连接对象。后面需要连接的话,直接从池子里面取,不需要去自己创建连接。使用完毕,记得归还连接,确保连接对象能够循环利用。
DBCP:
@Test
public void testDBCP01(){
Connection conn = null;
PreparedStatement ps = null;
try {
//构建数据源对象
BasicDataSource dataSource=new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/bank");
dataSource.setUsername("root");
dataSource.setPassword("123456");
conn = dataSource.getConnection();
String sql="insert into account values(null,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "admin");
ps.setInt(2, 1000);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtil.release(conn, ps);
}
}
使用属性配置文件:
@Test
public void testDBCP02() {
Connection conn = null;
PreparedStatement ps = null;
try {
BasicDataSourceFactory factory = new BasicDataSourceFactory();
Properties properties=new Properties();
FileInputStream is = new FileInputStream("src//dbcpconfig.properties");
properties.load(is);
DataSource dataSource=factory.createDataSource(properties);
conn = dataSource.getConnection();
String sql="insert into account values(null,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "xiaoliang");
ps.setInt(2, 100);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtil.release(conn, ps);
}
}
C3P0
@Test
public void testC3P0() {
Connection conn = null;
PreparedStatement ps = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost/bank");
dataSource.setUser("root");
dataSource.setPassword("123456");
conn = dataSource.getConnection();
String sql="insert into account values(null,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "admin01");
ps.setInt(2, 1000);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtil.release(conn, ps);
};
}
使用配置文件。配置文件必须是c3p0-config.xml
@Test
public void testC3P0() {
Connection conn = null;
PreparedStatement ps = null;
try {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
conn = dataSource.getConnection();
String sql="insert into account values(null,?,?)";
ps = conn.prepareStatement(sql);
ps.setString(1, "admin02");
ps.setInt(2, 1000);
ps.executeUpdate();
} catch (Exception e) {
// TODO: handle exception
}finally {
JDBCUtil.release(conn, ps);
};
}
DBUtils实现增删改查
@Test
public void testInsert() throws SQLException, InstantiationException, IllegalAccessException {
ComboPooledDataSource dataSource = new ComboPooledDataSource();
QueryRunner queryRunner = new QueryRunner(dataSource);
//增加
//queryRunner.update("insert into account values (null,?,?)","aa",1000);
//删除
//queryRunner.update("delete from account where id =?",5);
//修改
//queryRunner.update("update account set money = ? where id =?",100000,6);
//查找
/*
* Account account=queryRunner.query("select * from account where id =?", new
* ResultSetHandler<Account>() {
*
* @Override public Account handle(ResultSet rs) throws SQLException { // TODO
* Auto-generated method stub Account account = new Account(); while(rs.next())
* { String name = rs.getString("name"); int money = rs.getInt("money");
* account.setName(name); account.setMoney(money); } return account; }
*
* },6);
* System.out.println(account.toString());
*/
//查一条
Account account=queryRunner.query("select * from account where id=?", new BeanHandler<Account>(Account.class),6);
System.out.println(account);
//查多条
List<Account> list = queryRunner.query("select * from account", new BeanListHandler<Account>(Account.class));
System.out.println(list);
}
//通过类的字节码得到该类的实例
Account a = new Account();
//创建一个类的实例
Account a1 = Account.class.newInstance();