准备:导包
1.创建c3p0-config.xml配置文件放在src下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- default-config 默认的配置, -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost/mydb</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
<property name="maxStatements">200</property>
</default-config>
<!-- This app is massive! -->
<named-config name="oracle">
<property name="acquireIncrement">50</property>
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
<!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property>
<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe">
<property name="acquireIncrement">1</property>
<property name="initialPoolSize">1</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
<property name="maxStatementsPerConnection">50</property>
</user-overrides>
</named-config>
</c3p0-config>
2.创建一个bean
package rick;
public class UserStus {
private String name;
private int money;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getMoney() {
return money;
}
public void setMoney(int money) {
this.money = money;
}
@Override
public String toString() {
return "UserStus [name=" + name + ", money=" + money + "]";
}
}
3.测试DBUtils
package rick;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/*
//针对增加 、 删除 、 修改
queryRunner.update(sql)
//针对查询
queryRunner.query(sql, rsh);*/
public class DBUtilsTest {
@Test
public void testdemo() throws SQLException {
//建立连接(C3P0数据库连接池的方式)
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//创建DBUtils
QueryRunner queryRunner = new QueryRunner(dataSource);
// 添加
String name = "qirui1";
int money = 10000;
queryRunner.update("insert into stus values (?,?)", name, money);
// 删除
queryRunner.update("delete from stus where name = ?", "admin");
// 更新
queryRunner.update("update stus set money = ? where name = ?", 10000000, name);
// 查询单个对象
UserStus userstu = queryRunner.query("select * from stus where name = ?",new BeanHandler<UserStus>(UserStus.class), "lisi");
System.out.println(userstu.toString());
//查询所有对象
List<UserStus> list = queryRunner.query("select * from stus ", new BeanListHandler<UserStus>(UserStus.class));
for (UserStus userstus : list) {
System.out.println(userstus.toString());
}
}
}
总体结构: