1. 程式人生 > >c3p0簡介及簡單案例

c3p0簡介及簡單案例

c3p0所需jar包和配置檔案下載地址:點選開啟連結

1C3P0簡介

  C3P0也是開源免費的連線池!C3P0被很多人看好!

2C3P0的使用

  C3P0中池類是:ComboPooledDataSource。

public void fun1() throws PropertyVetoException, SQLException {
ComboPooledDataSource ds = new ComboPooledDataSource();
ds.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");
ds.setUser("root");
ds.setPassword("123");
ds.setDriverClass("com.mysql.jdbc.Driver");
ds.setAcquireIncrement(5);
ds.setInitialPoolSize(20);
ds.setMinPoolSize(2);
ds.setMaxPoolSize(50);
Connection con = ds.getConnection();
System.out.println(con);
con.close();
}


配置檔案要求: 

l 檔名稱:必須叫c3p0-config.xml

l 檔案位置:必須在src


1.JdbcUtils.java

package cn.itcast.jdbc;

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

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class JdbcUtils {
	// 配置檔案的預設配置!要求你必須給出c3p0-config.xml!!!
	private static ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	/**
	 * 使用連線池返回一個連線物件
	 * @return
	 * @throws SQLException
	 */
	public static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
	}
	
	/**
	 * 返回連線池物件!
	 * @return
	 */
	public static DataSource getDataSource() {
		return dataSource;
	}
}

2.Demo3.java
package cn.itcast.dbutils;

import java.sql.SQLException;
import java.util.List;
import java.util.Map;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;

import cn.itcast.jdbc.JdbcUtils;

public class Demo3 {
	@Test
	public void fun1() throws SQLException {
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "insert into t_stu values(?,?,?,?)";
		Object[] params = {1002, "liSi", 88, "female"};
		
		qr.update(sql, params);
	}
	
	@Test
	public void fun2() throws SQLException {
		// 建立QueryRunner,需要提供資料庫連線池物件
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		// 給出sql模板
		String sql = "select * from t_stu where sid=?";
		// 給出引數
		Object[] params = {1001};
		
//		ResultSetHandler<Stu> rsh = new ResultSetHandler<Stu>() {
//
//			@Override
//			public Stu handle(ResultSet rs) throws SQLException {
//				// TODO Auto-generated method stub
//				return null;
//			}
//		};
		// 執行query()方法,需要給出結果集處理器,即ResultSetHandler的實現類物件
		// 我們給的是BeanHandler,它實現了ResultSetHandler
		// 它需要一個型別,然後它會把rs中的資料封裝到指定型別的javabean物件中,然後返回javabean
		Stu stu = qr.query(sql, new BeanHandler<Stu>(Stu.class), params);
		System.out.println(stu);
	}
	
	/**
	 * BeanListHandler的應用,它是多行處理器
	 * 每行物件一個Stu物件!
	 * @throws Exception
	 */
	@Test
	public void fun3() throws Exception {
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select * from t_stu";
		List<Stu> stuList = qr.query(sql, new BeanListHandler<Stu>(Stu.class));
		
		System.out.println(stuList);
	}
	
	/**
	 * MapHandler的應用,它是單行處理器,把一行轉換成一個Map物件
	 * @throws SQLException 
	 */
	@Test
	public void fun4() throws SQLException  {
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select * from t_stu where sid=?";
		Object[] params = {1001};
		Map map = qr.query(sql, new MapHandler(), params);
		
		System.out.println(map);
	}
	
	/**
	 * MapListHandler,它是多行處理器,把每行都轉換成一個Map,即List<Map>
	 * @throws SQLException
	 */
	@Test
	public void fun5() throws SQLException  {
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select * from t_stu";
		List<Map<String,Object>> mapList = qr.query(sql, new MapListHandler());
		
		System.out.println(mapList);
	}
	
	/**
	 * ScalarHandler,它是單行單列時使用,最為合適!
	 * @throws SQLException
	 */
	@Test
	public void fun6() throws SQLException {
		QueryRunner qr = new QueryRunner(JdbcUtils.getDataSource());
		String sql = "select count(*) from t_stu";
		/*
		 * Integer、Long、BigInteger
		 */
		Number cnt = (Number)qr.query(sql, new ScalarHandler());
		
		long c = cnt.longValue();
		System.out.println(c);
	}
}

3.c3p0-config.xml(名字不能變)
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
	<!-- 這是預設配置資訊 -->
	<default-config> 
		<!-- 連線四大引數配置 -->
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/mydb3</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>