c3p0数据库连接池使用步骤c3p0配置报错处理
一、导入jar包下载:Mavenc3p0-0.9.1.2.jar二、配置文件c3p0详细配置(c3p0-config.xml)<?xml version="1.0" encoding="UTF-8"?><c3p0-config><default-config><property name="jdbcUrl">jdbc:mysql://localhos
·
一、导入jar包
下载:Maven
c3p0-0.9.1.2.jar
二、配置文件
c3p0详细配置(c3p0-config.xml)
c3p0-config.xml名称必须一模一样
<?xml version=“1.0” encoding=“UTF-8”?>必须在文件的第一行
中文编码,&符号要写成&
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3308/shopping?useUnicode=true&characterEncoding=UTF-8</property>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">123456</property>
<!-- 初始化大小-->
<property name="initialPoolSize">3</property>
<!-- 最多有多少个连接-->
<property name="maxPoolSize">6</property>
<!-- 最少几个连接-->
<property name="mPoolSize">2</property>
<!-- 最大空闲数-->
<property name="maxIdleTime">1000</property>
<!-- 每次最多可以执行多少个批处理语句-->
<property name="maxStatements">50</property>
</default-config>
<named-config name="demo">
<property name="user">root</property>
<property name="password">123456</property>
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3308/shopping?useUnicode=true&characterEncoding=UTF-8</property>
<property name="initialPoolSize">10</property>
<property name="maxIdleTime">30</property>
<property name="maxPoolSize">100</property>
<property name="minPoolSize">10</property>
</named-config>
</c3p0-config>
三、 写C3p0Utils工具类(具有初始化连接池,获得连接的方法)
// 新建数据库连接池,注意这里的参数必须同c3p0-config.xml中的named-config标签中的name的值相同
// 如果不写,则默认调用default-config
package com.itqf.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import java.sql.Connection;
import java.sql.SQLException;
public class C3p0Utils {
private static ComboPooledDataSource dataSource = null;
static {
dataSource = new ComboPooledDataSource();
}
public static ComboPooledDataSource getDataSource() {
return dataSource;
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
四、C3p0Utils在Dao层使用
package com.itqf.dao.impl;
import com.itqf.dao.UserDao;
import com.itqf.entity.User;
import com.itqf.utils.C3p0Utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import java.sql.Connection;
import java.sql.SQLException;
/*
* 用户模块数据库访问的实现类*/
public class UserDaoImpl implements UserDao {
private QueryRunner queryRunner = new QueryRunner(C3p0Utils.getDataSource());
@Override
public User selectUserByUname(String username) {
User user = null;
String sql = "select u_id as uid,u_name as uname,u_password as upassword" +
"u_email as uemail,u_sex as usex,u_status as status ,u_code as ucode" +
"u_role as urole from user where u_name = ?";
try {
user = queryRunner.query(sql, new BeanHandler<User>(User.class), username);
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
}
更多推荐
已为社区贡献4条内容
所有评论(0)