前言:连接池的作用详见:https://blog.csdn.net/aiming66/article/details/82938398
C3P0开源免费的连接池!目前使用它的开源项目有:Spring、Hibernate等。使用第三方工具需要导入jar包,C3P0使用时还需要添加配置文件c3p0-config.xml。
myeclipse
jdbc相关jar
C3P0相关jar包
注意事项:
配置文件名:c3p0-config.xml(固定)
配置文件位置:src (类路径)
配置文件的内容如下:
<c3p0-config>
<default-config> <--默认配置方式--!>
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql:///web_07property>
<property name="user">rootproperty>
<property name="password">123property>
<property name="initialPoolSize">5property>
<property name="maxPoolSize">20property>
default-config>
<named-config name="oracle"> <--以oracle命名配置方式--!>
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql:///web_07property>
<property name="user">rootproperty>
<property name="password">123property>
named-config>
c3p0-config>
c3p0test.java 中的代码
package it.c3p0;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class c3p0test {
@Test
public void testAdduser(){
Connection conn=null;
PreparedStatement pstmt=null;
//1、创建连接池对象
ComboPooledDataSource datasource=new ComboPooledDataSource();//实例化后,自动加载c3p0-config.xml文件。
// 实例化后,自动加载c3p0-config.xml文件中的默认配置
//ComboPooledDataSource datasource=new ComboPooledDataSource("oracle");实例化后,自动加载c3p0-config.xml文件中的以“oracle”命名的配置
//2、连接池中获取链接
try {
conn=datasource.getConnection();
String sql="insert into user values(?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"test1");
pstmt.setString(2, "test1pwd");
int rows=pstmt.executeUpdate();
if(rows>0){System.out.print("添加成功");}
else{System.out.print("添加失败");}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{// 关闭连接
try {
if(pstmt!=null)pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
配置文件中的代码:
<c3p0-config>
<default-config>
<property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydbproperty>
<property name="user">rootproperty>
<property name="password">rootproperty>
<property name="initialPoolSize">5property>
<property name="maxPoolSize">20property>
default-config>
<named-config name="oracle">
<property name="driverClass">com.mysql.jdbc.Driverproperty>
<property name="jdbcUrl">jdbc:mysql:///web_07property>
<property name="user">rootproperty>
<property name="password">123property>
named-config>
c3p0-config>
仔细分析下面代码,我们大部分操作数据库时,仅仅有下面红框的部分不懂,那么我们可以使用一个工具类将其他的封装起来。
步骤:
新建C3P0utils工具类
C3P0utils.java
package it.utils;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0utils {
private static ComboPooledDataSource datasource=new ComboPooledDataSource();
public static DataSource getDataSource(){
return datasource;
}
public static Connection getConnection(){
try {
return datasource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
C3P0testV_2.java
package it.c3p0;
import it.utils.C3P0utils;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import org.junit.Test;
public class C3P0testV_2 {
@Test
public void testAdduser(){
Connection conn=null;
PreparedStatement pstmt=null;
//2、连接池中获取链接
try {
conn=C3P0utils.getConnection();
String sql="insert into user values(?,?)";
pstmt=conn.prepareStatement(sql);
pstmt.setString(1,"test1");
pstmt.setString(2, "test1pwd");
int rows=pstmt.executeUpdate();
if(rows>0){System.out.print("添加成功");}
else{System.out.print("添加失败");}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{// 关闭连接
try {
if(pstmt!=null)pstmt.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
if(conn!=null)conn.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
DBCP也是一个开源的连接池!是ApacheCommon成员之一,在企业开发中也比较常见,
tomcat内置连接池。
myeclipse
jdbc相关jar
DBCP相关jar包
配置文件名称:*.properties
配置文件位置:任意,建议src(classpath/类路径)
配置文件内容:properties不能编写中文,不支持在STS中修改,必须使用记事本修改内容,否则中文注释就乱码了
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/jdbc
username=root
password=
#
initialSize=10
#最大连接数量
maxActive=50
#
maxIdle=20
#
minIdle=5
#
maxWait=60000
#JDBC驱动建立连接时附带的连接属性属性的格式必须为这样:[属性名=property;]
#注意:"user" 与 "password" 两个属性会被明确地传递,因此这里不需要包含他们。
connectionProperties=useUnicode=true;characterEncoding=gbk
#指定由连接池所创建的连接的自动提交(auto-commit)状态。
defaultAutoCommit=true
#driver default 指定由连接池所创建的连接的事务级别(TransactionIsolation)。
#可用值为下列之一:(详情可见javadoc。)NONE,READ_UNCOMMITTED, READ_COMMITTED, REPEATABLE_READ, SERIALIZABLE
defaultTransactionIsolation=READ_UNCOMMITTED
4、工具类
package cn.itheima.jdbc.utils;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
public class DBCPUtils {
private static DataSource dataSource;
static{
try {
//1.加载找properties文件输入流
InputStream is = DBCPUtils.class.getClassLoader().getResourceAsStream("db.properties");
//2.加载输入流
Properties props = new Properties();
props.load(is);
//3.创建数据源
dataSource = BasicDataSourceFactory.createDataSource(props);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public static DataSource getDataSource(){
return dataSource;
}
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
测试类文件:
package cn.itheima.jdbc.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import cn.itheima.jdbc.utils.DBCPUtils;
public class TestDBCP {
@Test
public void testUpdateUserById(){
Connection conn = null;
PreparedStatement pstmt = null;
try {
conn = DBCPUtils.getConnection();
String sql ="update tbl_user set upassword=? where uid=?";
pstmt= conn.prepareStatement(sql);
pstmt.setString(1, "柳岩");
pstmt.setInt(2, 20);
int rows = pstmt.executeUpdate();
if(rows>0){
System.out.println("更新成功!");
}else{
System.out.println("更新失败!");
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}