需要准备的jar包:
MySQL的jar包mysql-connector-Java-5.1.39-bin目前是5.1.39版本:
http://dev.mysql.com/downloads/connector/j/
C3p0的2个包:
https://sourceforge.net/projects/c3p0/
c3p0-0.9.1.2.jar和c3p0-sources-0.9.1.2.ja
<c3p0-config>
<!-- 默认配置,如果没有指定则使用这个配置 -->
<default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">
<![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]>
</property>
<property name="user">root</property>
<property name="password">1234</property>
<!-- 初始化池大小 -->
<property name="initialPoolSize">2</property>
<!-- 最大空闲时间 -->
<property name="maxIdleTime">30</property>
<!-- 最多有多少个连接 -->
<property name="maxPoolSize">10</property>
<!-- 最少几个连接 -->
<property name="minPoolSize">2</property>
<!-- 每次最多可以执行多少个批处理语句 -->
<property name="maxStatements">50</property>
</default-config>
<!-- 命名的配置 -->
<named-config name="demo">
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl"><![CDATA[jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=UTF-8]]></property>
<property name="user">root</property>
<property name="password">1234</property>
<property name="acquireIncrement">5</property><!-- 如果池中数据连接不够时一次增长多少个 -->
<property name="initialPoolSize">100</property>
<property name="minPoolSize">50</property>
<property name="maxPoolSize">1000</property>
<property name="maxStatements">0</property>
<property name="maxStatementsPerConnection">5</property> <!-- he's important, but there's only one of him -->
</named-config>
</c3p0-config>
- 1
- 2
- 3
- 4
- 5
- 6
- 8
- 9
演示代码:
package cn.hncu.C3p0;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Demo {
@Test
// 纯Java方法使用c3p0
public void C3p0Demo() throws PropertyVetoException, SQLException {
ComboPooledDataSource pool = new ComboPooledDataSource();
pool.setUser("root");// 用户姓名
pool.setPassword("1234");// 用户密码
pool.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/hncu?useUnicode=true&characterEncoding=utf-8");// MySQL数据库连接url
pool.setDriverClass("com.mysql.jdbc.Driver");
// pool可以调用set方法进行Connection池的配置
// 连接关闭之后,内存会被释放,下次取时会重新开(内存地址不共用)
for (int i = 0; i < 20; i++) {
Connection con = pool.getConnection();
System.out.println(i + ":" + con);
if (i % 2 == 0) {
con.close();
}
}
}
@Test
// 演示采用配置文件的方式使用c3p0
public void C3p0PropertyDemo() throws SQLException {
ComboPooledDataSource pool = new ComboPooledDataSource();//空参,自动到classpath目录下面加载“c3p0-config.xml”配置文件---配置文件的存储位置和名称必须是这样,且使用“默认配置”
//ComboPooledDataSource pool = new ComboPooledDataSource("demo");//加载“c3p0-config.xml”文件中定义的“demo”这个配置元素
for(int i=0;i<25;i++){
Connection con = pool.getConnection();
System.out.println(i+":"+ con.hashCode());
}
}
}
获得Connection
package cn.hncu.C3p0;
import java.sql.Connection;
import java.sql.SQLException;
import javax.sql.DataSource;
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3p0Pool{
private static DataSource dataSource = null;
static{
//dataSource资源只能初始化一次
dataSource= new ComboPooledDataSource("mvcApp");
}
//获取链接
public static Connection getcon(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("链接池出错!");
}
}
//关闭链接
public static void closeCon(Connection con){
try {
if(con!=null){
con.close();
}
} catch (SQLException e) {
throw new RuntimeException("关闭数据库链接失败!");
}
}
}