c3p0数据源

5 篇文章 0 订阅

需要读取的配置文件c3p0-cinfig.xml

<c3p0-config>
	<!-- c3p0-config.xml的默认配置,在classpath下-->
	<!-- 如果要使用自己的配置,需要该name属性 -->
	<default-config>
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
		<property name="user">root</property>
		<property name="password">mysqladmin</property>
		
		<property name="initialPoolSize">10</property>
		<property name="maxIdleTime">30</property>
		<property name="maxPoolSize">20</property>
		<property name="minPoolSize">5</property>
		<property name="maxStatements">200</property>
	</default-config>
	
	<!-- 自己的配置 -->
	<named-config name="mysql">
		<property name="driverClass">com.mysql.jdbc.Driver</property>
		<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
		<property name="user">root</property>
		<property name="password">mysqladmin</property>
		
		<property name="acquireIncrement">50</property>
		<property name="initialPoolSize">100</property>
		<property name="minPoolSize">50</property>
		<property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
		<property name="maxStatements">0</property>
		<property name="maxStatementsPerConnection">5</property>
	</named-config>
	
	<named-config name="oracle">
		<property name="acquireIncrement">50</property>
		<property name="initialPoolSize">100</property>
		<property name="minPoolSize">50</property>
		<property name="maxPoolSize">1000</property><!-- intergalactoApp adopts a different approach to configuring statement caching -->
		<property name="maxStatements">0</property>
		<property name="maxStatementsPerConnection">5</property>
	</named-config>
</c3p0-config>
操作数据库的工具类JdbcUtils.java
import com.mchange.v2.c3p0.ComboPooledDataSource;
public class JdbcUtils{
	private static ComboPooledDataSource ds = null;
	static{
		try{
			//c3p0-config.xml配置文件在classpath下
			ds = new ComboPooledDataSource("mysql");	//使用c3p0-config.xml中的mysql的配置,
//			ds = new ComboPooledDataSource();	//使用c3p0-config.xml的默认配置
			
			//如果不使用c3p0-cinfig.xml配置文件,自己写配置
//			ds.setDriverClass("com.mysql.jdbc.Driver");
//			ds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
//			ds.setUser("root");
//			ds.setPassword("mysqladmin");
//			ds.setMaxPoolSize(30); //最大允许30个链接
//			ds.setMinPoolSize(5);	//最小允许5个链接
//			ds.setInitialPoolSize(10);	//初始化链接个数
		}catch (Exception e) {
			throw new ExceptionInInitializerError(e);
		}
		
	}
	
	public static Connection getConnection() throws SQLException{	
		return ds.getConnection();	//从c3p0数据源中获得数据库链接
	}
	
	
	public static void release(Connection conn,Statement st,ResultSet rs){	
		if(rs != null){
			try{
				rs.close();	
			}catch(Exception e){}
		}
		if(st != null){
			try{
				st.close();
			}catch(Exception e){}
		}
		if(conn != null){
			try{
				conn.close();//从c3p0数据源中获得数据库链接的close方法被增强,将数据库链接归还给连接池
			}catch(Exception e){}
		}
	}
}
test.java

public class test {

	public static void main(String[] args) {
		Connection conn = null;
		PreparedStatement st= null;
		ResultSet rs = null;
		
		try{
			conn = JdbcUtils.getConnection();	// 从c3p0数据源中获得链接
			System.out.println(conn.getClass().getName());
		}catch (Exception e) {
			// TODO: handle exception
		}
		JdbcUtils.release(conn, st, rs);//释放链接,将链接归还到连接池
	}
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值