首页 > 代码库 > Java-C3P0连接池

Java-C3P0连接池

使用c3p0连接池,不需要自己解析配置文件,只需要在src文件夹下创建指定名称的配置文件即可。

 

创建项目:

技术分享

 

c3p0.xml配置文件:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<!-- 默认配置,如果没有指定则使用这个配置 -->
	<default-config>
		<!-- 基本配置 -->
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb</property>
		<property name="user">root</property>
		<property name="password">root</property>
	
		<!--扩展配置-->
		<property name="checkoutTimeout">30000</property>
		<property name="idleConnectionTestPeriod">30</property>
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">100</property>
		<property name="minPoolSize">10</property>
		<property name="maxStatements">200</property>
	</default-config> 
	
	
	<!-- 命名的配置 -->
	<named-config name="localhost">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/mydb</property>
		<property name="user">root</property>
		<property name="password">root</property>
		
		
		<!-- 如果池中数据连接不够时一次增长多少个 -->
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">20</property>
		<property name="minPoolSize">10</property>
		<property name="maxPoolSize">40</property>
		<property name="maxStatements">20</property>
		<property name="maxStatementsPerConnection">5</property>
	</named-config>
	
	<!-- 命名的配置2 -->
	<named-config name="itcast">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/XXX</property>
		<property name="user">root</property>
		<property name="password">root</property>
		
		
		<!-- 如果池中数据连接不够时一次增长多少个 -->
		<property name="acquireIncrement">5</property>
		<property name="initialPoolSize">20</property>
		<property name="minPoolSize">10</property>
		<property name="maxPoolSize">40</property>
		<property name="maxStatements">20</property>
		<property name="maxStatementsPerConnection">5</property>
	</named-config>
	
	<!-- 可以有很多命名的配置 X  -->
	
</c3p0-config> 

  

C3P0Test.java:

package com.gordon.c3p0;

import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayListHandler;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Test {
	public static void main(String[] args) {
		try {
			
			selectFunction();
			
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}

	public static void selectFunction() throws Exception {
		// 可以使用多种命令配置,如果没有找到则使用默认的配置
		ComboPooledDataSource cpds = new ComboPooledDataSource("localhost");

		// 使用DBUtils
		QueryRunner qr = new QueryRunner(cpds);

		// structure query language
		String sql = "SELECT * FROM user";

		// 设置参数
		Object[] params = {};

		// 执行sql
		List<Object[]> objList = qr.query(sql, new ArrayListHandler(), params);

		// 操作结果
		for (Object[] obj : objList) {
			System.err.println(obj[0] + ":" + obj[1]);
		}
	}
}

  

Java-C3P0连接池