java----c3p0連接池


C3p0連接池。目前使用它的開源項目有Spring,Hibernate等。使用第三方工具需要
* 導入jar包,c3p0使用時還需要添加配置文件c3p0-config.xml
* 配置文件名稱:c3po-config.xml(固定)
* 配置文件位置:src(類路徑)
* 配置文件內容:命名配置
* c3p0的核心工具類:ComboPooledDataSource.如果要使用連接池,必須創建該類的實例對象。

c3p0-config.xml文件相關說明

 

import java.sql.Connection;

import javax.management.RuntimeErrorException;
import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class Mtest8Demo {
	/*
	 * C3p0連接池。目前使用它的開源項目有Spring,Hibernate等。使用第三方工具需要
	 * 導入jar包,c3p0使用時還需要添加配置文件c3p0-config.xml
	 * 配置文件名稱:c3po-config.xml(固定)
	 * 配置文件位置:src(類路徑)
	 * 配置文件內容:命名配置
	 * c3p0的核心工具類:ComboPooledDataSource.如果要使用連接池,必須創建該類的實例對象。
	 */
	//使用c3p0的默認配置
	//public static ComboPooledDataSource dataSource=new ComboPooledDataSource();
	
	//使用命名配置
	public static ComboPooledDataSource dataSource=new ComboPooledDataSource("test");
	
	/*
	 * 獲得數據源(連接池)
	 */
	public static DataSource getDataSource() {
		return dataSource;
	}
	
	//獲得連接
	public static Connection getConnection() {
		try {
			return dataSource.getConnection();
		} catch (Exception e) {
			// TODO: handle exception
			throw new RuntimeException(e);
		}
	}
}

  

c3p0-config.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:///study</property>
	<property name="user">root</property>
	<property name="password">root</property>
	<property name="initialPoolSize">5</property>
	<property name="maxPoolSize">20</property>
  </default-config>
  
  <named-config name="test"> 
    <property name="driverClass">com.mysql.jdbc.Driver</property>
	<property name="jdbcUrl">jdbc:mysql:///study</property>
	<property name="user">root</property>
	<property name="password">root</property>
  </named-config>
  

</c3p0-config>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM