c3p0-config.xml 文件 放在在src下
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="acquireIncrement">2</property>
<property name="initialPoolSize">5</property>
<property name="minPoolSize">1</property>
<property name="maxPoolSize">5</property>
</default-config>
</c3p0-config>
dbutil.java 源代码
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class dbutil {
private static ComboPooledDataSource dataSource=new ComboPooledDataSource();
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
public static void setDataSource(ComboPooledDataSource dataSource) {
dbutil.dataSource = dataSource;
}
}
dao程序需要第三方 commons-dbutils-1.4.jar jar包
import org.apache.commons.dbutils.QueryRunner;
public class EmpsDao {
public void add(Emps emp) throws Exception{
QueryRunner runner=new QueryRunner(dbutil.getDataSource());
String sql="insert into emps(id,username,salary) values(?,?,?)";
Object[] param={emp.getId(),emp.getUsername(),emp.getSalary()};
runner.update(sql, param);
//dbutil.getDataSource().getConnection();
}
public static void main(String[] args) {
Emps emp=new Emps("zhangsan",400.34);
EmpsDao e=new EmpsDao();
try {
e.add(emp);
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
}