简单使用c3p0连接池

首先,c3p0是一个连接池插件

需要jar包:

  

使用手动配置:

/**
     * 手动配置使用c3p0
     * @throws PropertyVetoException
     * @throws SQLException
     */
    @Test
    public void fun1() throws PropertyVetoException, SQLException{
        //创建连接池对象
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        
        //对池进行四大参数的配置
        dataSource.setDriverClass("com.mysql.jdbc.Driver");
        dataSource.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/demo");
        dataSource.setUser("guodaxia");
        dataSource.setPassword("961012gz");
        
        //池配置
        dataSource.setAcquireIncrement(5);
        dataSource.setInitialPoolSize(20);
        dataSource.setMinPoolSize(2);
        dataSource.setMaxPoolSize(50);
        
        Connection con=dataSource.getConnection();
        System.out.println(con);
        con.close();
    }
    

c3p0允许通过xml配置,类似于hibernate.cfg.xml一样:

c3p0配置要求:
文件名称:必须叫c3p0-config.xml
文件位置:必须在src下

c3p0-config.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<c3p0-config>
    <!-- 默认连接配置 -->
    <default-config> 
        <!-- 连接四大参数配置  -->
        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/demo</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">guodaxia</property>
        <property name="password">961012gz</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"> <!-- 注意,这里是named-config而不是name-config,否则取读不到配置信息 -->
        <!-- 连接四大参数配置  -->
        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/demo</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">guodaxia</property>
        <property name="password">961012gz</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>

使用配置文件中的默认配置:

/**
     * 配置文件的默认配置
     * @throws SQLException 
     */
    @Test
    public void fun2() throws SQLException{
        /**
         * 在创建连接池对象的时候,对象就会自动加载配置文件,不需要我们指定
         */
        ComboPooledDataSource dataSource=new ComboPooledDataSource();
        Connection con=dataSource.getConnection();
        System.out.println(con);
        con.close();
    }
    

配置文件中可以配置多歌配置信息,可以手动选择:

使用指定配置:

/**
     * 使用命名配置
     * @throws SQLException 
     */
    @Test
    public void fun3() throws SQLException{
        /**
         * 构造器的参数指定了命名配置元素的鄂明成
         * <name-config name="oracle-config">
         */
        ComboPooledDataSource dataSource=new ComboPooledDataSource("oracle-config");
        Connection con=dataSource.getConnection();
        System.out.println(con);
        con.close();
    }

c3p0之后的jdbc连接池:

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。xnl!
     */
    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;
    }
}
原文地址:https://www.cnblogs.com/aigeileshei/p/5714463.html