c3p0-config.xml配置
<default-config>
<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/emp</property>
<property name="user">root</property>
<property name="password">123</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">8</property>
<property name="checkoutTimeout">3000</property>
</default-config>
DBUtil代码
package bd.login.util;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class DButil {
private static ComboPooledDataSource ds=new ComboPooledDataSource();
public static DataSource getDataresource() {
return ds;
}
public static QueryRunner getQueryRunner() {
return new QueryRunner(ds);
}
}
执行代码
package bd.login.dao;
import java.sql.SQLException;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import bd.login.entry.Admin;
import bd.login.util.DButil;
public class AdminDaoImpl {
public Admin login(int i) {
String sql = "SELECT * FROM admin WHERE id=?";
try {
return DButil.getQueryRunner().query(sql, new BeanHandler<Admin>(Admin.class),i);
} catch (Exception e) {
throw new RuntimeException("登陆时的DAO方法出错");
}
}
public static void main(String[] str) {
AdminDaoImpl a=new AdminDaoImpl();
a.login(1);
System.out.println(a);
}
报错信息,后面就是重要的,
信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30
要配置一个连接数不够用时候的增量属性:acquireIncrement ;
谢谢你,确实如此!