JDBC_c3p0连接池
Posted 跳动de手指
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JDBC_c3p0连接池相关的知识,希望对你有一定的参考价值。
package cn.code.demo; import java.beans.PropertyVetoException; import java.sql.Connection; import java.sql.SQLException; import org.junit.Test; import com.mchange.v2.c3p0.ComboPooledDataSource; /* * c3p0 * c3p0配置文件要求: * 文件名称必须是 c3p0-config.xml * 位置必须在src下 * */ public class C3P0Demo1 { @Test public void fun1() throws PropertyVetoException, SQLException{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); dataSource.setDriverClass("com.mysql.jdbc.Driver"); dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1"); dataSource.setUser("root"); dataSource.setPassword("123"); //池配置 // dataSource.setInitialPoolSize(20); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } @Test public void fun2() throws SQLException{ ComboPooledDataSource dataSource = new ComboPooledDataSource(); Connection con = dataSource.getConnection(); System.out.println(con); con.close(); } }
//配置文件
<?xml version="1.0" encoding="UTF-8"?> <c3p0-config> <!-- 默认配置信息 --> <default-config> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </default-config> <!-- 自定义配置信息(模拟oracle) --> <named-config name="oracle-config"> <property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb1</property> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="user">root</property> <property name="password">123</property> <property name="acquireIncrement">3</property> <property name="initialPoolSize">10</property> <property name="minPoolSize">2</property> <property name="maxPoolSize">10</property> </named-config> </c3p0-config>
以上是关于JDBC_c3p0连接池的主要内容,如果未能解决你的问题,请参考以下文章