码迷,mamicode.com
首页 > 数据库 > 详细

JDBC_c3p0连接池

时间:2017-08-25 15:56:05      阅读:195      评论:0      收藏:0      [点我收藏+]

标签:配置   bsp   class   tin   void   close   c3p0   pool   package   

package cn.code.demo;

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/*
 * c3p0
 * c3p0配置文件要求:
 * 文件名称必须是 c3p0-config.xml
 * 位置必须在src下
 * */
public class C3P0Demo1 {
    @Test
    public void fun1() throws PropertyVetoException, SQLException{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/mydb1");
        dataSource.setUser("root");
        dataSource.setPassword("123");
        //池配置
//        dataSource.setInitialPoolSize(20);
        
        Connection con = dataSource.getConnection();
        System.out.println(con);
        con.close();
    }
    @Test
    public void fun2() throws SQLException{
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        Connection con = dataSource.getConnection();
        System.out.println(con);
        con.close();
    }
}

//配置文件

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<!-- 默认配置信息 -->
    <default-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>
    </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>

 

JDBC_c3p0连接池

标签:配置   bsp   class   tin   void   close   c3p0   pool   package   

原文地址:http://www.cnblogs.com/wangyinxu/p/7428222.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!