jdbc数据库连接池

1.c3p0数据库连接池包的引入
在这里插入图片描述
2.建立jdbcpool.java文件
package com.dy.pool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.DataSources;

public class JDBCPool {

   public static void main(String[] args) {
	   PreparedStatement ps=null;
	   ResultSet rs=null;
	 try {
		 //注册驱动
		Class.forName("oracle.jdbc.OracleDriver");
		//获得非池化的数据源
		DataSource unpoolds=DataSources.unpooledDataSource("jdbc:oracle:thin:@127.0.0.1:1521:orcl2", "scott", "tiger");
		//把非池的数据源转换成池的数据源
		DataSource poolds=DataSources.pooledDataSource(unpoolds);
		//C3P0是在第一获得连接的时候来初始化连接池
		Connection conn=poolds.getConnection();
		ps=conn.prepareStatement("select * from person");
		rs=ps.executeQuery();
		while(rs.next()) {
			System.out.println("ID:"+rs.getString(1)+"姓名"+rs.getString(2));
		}

	} catch (Exception e) {
		
		e.printStackTrace();
	}finally {
		try {
			if(rs!=null) {
				rs.close();
			}
			if(ps!=null) {
				ps.close();
			}
		} catch (Exception e) {
			
			e.printStackTrace();
	}
}

}}

3.c3p0-config.xml文件可以从c3p0包中去找

<?xml version="1.0" encoding="UTF-8"?> con_test 30000 30 10 30 100 10 200
<user-overrides user="test-user">
  <property name="maxPoolSize">10</property>
  <property name="minPoolSize">1</property>
  <property name="maxStatements">0</property>
</user-overrides>
50 100 50 1000
<!-- intergalactoApp adopts a different approach to configuring statement caching -->
<property name="maxStatements">0</property> 
<property name="maxStatementsPerConnection">5</property>

<!-- he's important, but there's only one of him -->
<user-overrides user="master-of-the-universe"> 
  <property name="acquireIncrement">1</property>
  <property name="initialPoolSize">1</property>
  <property name="minPoolSize">1</property>
  <property name="maxPoolSize">5</property>
  <property name="maxStatementsPerConnection">50</property>
</user-overrides>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值