c3p0的基本使用方法

c3p0应该说是十分常用的连接池了,很常用的dao层框架hibernate都是用c3p0的

他的用法相当简便,当然,要懂一点连接数据库的知识作为前提,直接上代码了。

至于jar包,可以直接百度,也可以到这里下载:Java连接数据库的工具类

下面是基本用法

package cn.bl.v4_DataSource.c3p0;

import java.sql.SQLException;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Demo1 {
	
	/**
	 * 方式1:使用纯Java方式
	 * @throws Exception
	 */
	@Test
	public void test1() throws Exception {
		ComboPooledDataSource source = new ComboPooledDataSource();
		source.setDriverClass("com.mysql.jdbc.Driver");
		source.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=utf-8");
		source.setUser("root");
		source.setPassword("000");
		
		//获取链接
		System.out.println(source.getConnection());
		
		/*输出如下信息(除了最后一行,前面的其实都是自动输出的基本信息):
			九月 23, 2018 11:40:45 上午 com.mchange.v2.log.MLog 
			信息: MLog clients using java 1.4+ standard logging.
			九月 23, 2018 11:40:45 上午 com.mchange.v2.c3p0.C3P0Registry banner
			信息: Initializing c3p0-0.9.1.2 [built 21-May-2007 15:04:56; debug? true; trace: 10]
			九月 23, 2018 11:40:45 上午 com.mchange.v2.c3p0.impl.AbstractPoolBackedDataSource getPoolManager
			信息: Initializing c3p0 pool... com.mchange.v2.c3p0.ComboPooledDataSource [ acquireIncrement -> 3, acquireRetryAttempts -> 30, acquireRetryDelay -> 1000, autoCommitOnClose -> false, automaticTestTable -> null, breakAfterAcquireFailure -> false, checkoutTimeout -> 0, connectionCustomizerClassName -> null, connectionTesterClassName -> com.mchange.v2.c3p0.impl.DefaultConnectionTester, dataSourceName -> 1hgf2td9y193nu61qxzvdh|160847b, debugUnreturnedConnectionStackTraces -> false, description -> null, driverClass -> com.mysql.jdbc.Driver, factoryClassLocation -> null, forceIgnoreUnresolvedTransactions -> false, identityToken -> 1hgf2td9y193nu61qxzvdh|160847b, idleConnectionTestPeriod -> 0, initialPoolSize -> 2, jdbcUrl -> jdbc:mysql://127.0.0.1:3306/aa?useUnicode=true&characterEncoding=utf-8, maxAdministrativeTaskTime -> 0, maxConnectionAge -> 0, maxIdleTime -> 30, maxIdleTimeExcessConnections -> 0, maxPoolSize -> 10, maxStatements -> 50, maxStatementsPerConnection -> 0, minPoolSize -> 2, numHelperThreads -> 3, numThreadsAwaitingCheckoutDefaultUser -> 0, preferredTestQuery -> null, properties -> {user=******, password=******}, propertyCycle -> 0, testConnectionOnCheckin -> false, testConnectionOnCheckout -> false, unreturnedConnectionTimeout -> 0, usesTraditionalReflectiveProxies -> false ]
			com.mchange.v2.c3p0.impl.NewProxyConnection@d64342
		 */
	}
	
	@Test
	public void test2() throws SQLException {
		//1.空参--默认配置
		ComboPooledDataSource ds = new ComboPooledDataSource();
		
		//2.带参--指定名称的配置
		//ComboPooledDataSource ds = new ComboPooledDataSource("BarryLee");
		
		System.out.println(ds.getConnection());
		//输出的信息类似test1
	}
	
	/*
	 * 测试c3p0工具类
	 */
	@Test
	public void testUtil() {
		System.err.println(C3P0Utils.getConnection());
	}
}

下面是配置文件,配置文件一定到放置到src下,命名为:c3p0-config.xml


	
	
		com.mysql.jdbc.Driver
		
			
		
		root
		000
		
		2
		
		30
		
		10
		
		2
		
		50
	 
	
	
		com.mysql.jdbc.Driver
		jdbc:mysql://127.0.0.1:3306/aa
		root
		000
		5
		100
		50
		1000
		0
		5 
	
 

下面是一个工具类

package cn.bl.v4_DataSource.c3p0;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Utils {
	private C3P0Utils() {}
	private static ComboPooledDataSource ds = null;
	private static ThreadLocaltLocal = new ThreadLocal<>();
	static {
		ds = new ComboPooledDataSource();//读取默认配置文件
	}
	public static DataSource getDataSource() {
		return ds;
	}
	public static Connection getConnection() {
		Connection con = tLocal.get();
		if(con==null) {
			try {
				con = ds.getConnection();
				tLocal.set(con);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return con;
	}
}

 

你可能感兴趣的:(java修炼笔记,数据库)