数据库连接池-C3P0
作者:互联网
1、将所需要的jar包导入lib目录下,需要如下2个jar包:
c3p0-0.9.5.5、mchange-commons-java-0.2.19
导入后注意是否加载成功。
2、配置.xml文档,与DBCP不同,C3P0使用.xml文档,且文档中可以同时配置多个配置信息
注意jdbcUrl中的内容使用&隔开
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- C3P0默认配置,dataSource = new ComboPooledDataSource(); -->
<default-config>
<!--jdbc驱动类-->
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<!--连接字符串-->
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai</property>
<property name="user">root</property>
<property name="password">123456</property>
<!--初始化创建的连接数量-->
<property name="initialPoolSize">10</property>
<!--最大的连接数量-->
<property name="maxPoolSize">20</property>
<property name="minPoolSize">5</property>
<property name="acquireIncrement">5</property>
</default-config>
<!-- C3P0命名配置,可以同时配置多套数据库的配置信息,dataSource = new ComboPooledDataSource("MySQL"); -->
<named-config name="MySQL">
<property name="driverClass">com.mysql.cj.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai</property>
<property name="user">root</property>
<property name="password">123456</property>
<property name="initialPoolSize">10</property>
<property name="maxPoolSize">20</property>
<property name="minPoolSize">5</property>
<property name="acquireIncrement">5</property>
</named-config>
</c3p0-config>
3、在utils包下创建JdbcUtils_C3P0类
C3P0的驱动代码相对简单,直接创建一个DataSource对象即可
dataSource = new ComboPooledDataSource("MySQL"); 驱动MySQL的配置
dataSource = new ComboPooledDataSource(); 驱动默认配置
直接使用dataSource.getConnection()即可获得连接
(也可手动配置驱动信息,不使用.xml文档,但相比较而言就会很麻烦)
package com.utils;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.apache.commons.dbcp2.BasicDataSourceFactory;
import javax.sql.DataSource;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
public class JdbcUtils_C3P0 {
private static DataSource dataSource = null;
//手动设置配置信息
//private static ComboPooledDataSource cpdataSource = null;
static {
try{
//配置的c3p0-config为xml文件,自动匹配加载
dataSource = new ComboPooledDataSource("MySQL");
//手动设置配置信息的代码:
/*cpdataSource = new ComboPooledDataSource();
cpdataSource.setDriverClass("com.mysql.cj.jdbc.Driver");
cpdataSource.setJdbcUrl("jdbc:mysql://localhost:3306/jdbcstudy?useUnicode=true&characterEncoding=utf8&useSSL=true&serverTimezone=Asia/Shanghai");
cpdataSource.setUser("root");
cpdataSource.setPassword("123456");
cpdataSource.setMaxPoolSize(20);*/
}catch (Exception e) {
e.printStackTrace();
}
}
//获取连接
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();//通过数据源获取连接
}
//释放连接资源
public static void release(Connection cn, PreparedStatement ps, ResultSet rs){
if(rs!=null){
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(ps!=null){
try {
ps.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if(cn!=null){
try {
cn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
4、创建测试类C3P0_Test
package com.jdbc;
import com.utils.JdbcUtils_C3P0;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class C3P0_Test {
public static void main(String[] args) {
Connection cn = null;
PreparedStatement ps = null;
ResultSet rs = null;
try {
cn = JdbcUtils_C3P0.getConnection();
//插入操作
String sql = "insert into users(id,name,password,email,birthday) values(?,?,?,?,?);";
ps = cn.prepareStatement(sql); //st对象是通过cn对象的方法获得
ps.setInt(1,9);
ps.setString(2,"demo5");
ps.setString(3,"123456");
ps.setString(4,"demo5@qq.com");
// java.util.Date为Java提供的jar包,先用new Date().getTime()获取本地时间戳
// java.sql.Date为数据库提供的jar包,再用sql提供的方法转换成sql可以识别的类型
ps.setDate(5,new java.sql.Date(new java.util.Date().getTime()));
int i = ps.executeUpdate();
if( i>0) {
System.out.println("插入成功!");
}
//查询操作
String sql2 = "select * from users";
rs = ps.executeQuery(sql2);
while (rs.next()){
// 对应表中字段名
System.out.println("id="+rs.getInt("id"));
System.out.println("name="+rs.getString("name"));
System.out.println("pwd="+rs.getString("password"));
System.out.println("email="+rs.getString("email"));
System.out.println("birth="+rs.getString("birthday"));
System.out.println("===============================================");
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
JdbcUtils_C3P0.release(cn,ps,rs);
}
}
}
标签:ps,java,amp,rs,数据库,C3P0,sql,import,连接池 来源: https://blog.csdn.net/Little_Sirius/article/details/122658668