C3P0数据源配置

一:添加依赖

复制代码
<dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
<dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>${c3p0.version}</version>
        </dependency>
复制代码

二:添加配置默认二种方式配置文件,XML。在源码com.mchange.v2.c3p0.cfg包下

2.1:c3p0.properties

复制代码
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/test
username=root
password=

initialPoolSize=10
maxIdleTime=50
maxPoolSize=20
minPoolSize=5
复制代码

2.2: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://localhost:3306/dev</property>
    <property name="user">root</property>
    <property name="password"></property>
 
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </default-config>
 
  <named-config name="mySource">
    <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"></property>
 
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </named-config>
</c3p0-config>
复制代码

三:配置数据源

复制代码
package com.jachs.c3p0.config;

import java.beans.PropertyVetoException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.cfg.C3P0ConfigUtils;

/***
 * 
 * @author zhanchaohan
 *
 */
public class C3p0Utill {
    
    /***
     * 加载Properties方式初始化数据源
     * @return
     * @throws FileNotFoundException
     * @throws IOException
     * @throws PropertyVetoException 
     * @see C3P0ConfigUtils
     */
    public ComboPooledDataSource initProperties() throws FileNotFoundException, IOException, PropertyVetoException {
        
        Properties properties=C3P0ConfigUtils.findResourceProperties();
        
        
        ComboPooledDataSource cpds=new ComboPooledDataSource();
        
//        cpds.setProperties(properties);//不起作用
        
        cpds.setDriverClass(properties.getProperty("driverClassName"));
        cpds.setJdbcUrl(properties.getProperty("url"));
        cpds.setUser(properties.getProperty("username"));
        cpds.setPassword(properties.getProperty("password"));
        
        cpds.setInitialPoolSize(Integer.parseInt(properties.getProperty("initialPoolSize")));
        cpds.setMaxIdleTime(Integer.parseInt(properties.getProperty("maxIdleTime")));
        cpds.setMaxPoolSize(Integer.parseInt(properties.getProperty("maxPoolSize")));
        cpds.setMinPoolSize(Integer.parseInt(properties.getProperty("minPoolSize")));
        
        return cpds;
    }
    /***
     * 
     * @return
     * @see C3P0ConfigXmlUtils
     */
    public ComboPooledDataSource initXML() {
//        ComboPooledDataSource cpds=new ComboPooledDataSource("mySource");
        ComboPooledDataSource cpds=new ComboPooledDataSource();//不给参数使用默认数据源
        return cpds;
    }
}
复制代码

四:测试

复制代码
package com.jachs.c3p0.config;

import java.beans.PropertyVetoException;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.junit.Before;
import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

/***
 * 
 * @author zhanchaohan
 *
 */
public class C3P0Test {
    private C3p0Utill c3p0Utill = new C3p0Utill();

    ComboPooledDataSource prCpds;
    ComboPooledDataSource xmlCpds;

    @Before
    public void init() throws FileNotFoundException, IOException, PropertyVetoException {
        prCpds = c3p0Utill.initProperties();
        xmlCpds = c3p0Utill.initXML();
    }

    @Test
    public void tetcPrCpds() throws SQLException {
        Connection connection = prCpds.getConnection();

        ResultSet resultSet = connection.prepareStatement("show tables").executeQuery();

        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }
    }
    @Test
    public void tetcxmlCpds() throws SQLException {
        Connection connection = xmlCpds.getConnection();

        ResultSet resultSet = connection.prepareStatement("show tables").executeQuery();

        while (resultSet.next()) {
            System.out.println(resultSet.getString(1));
        }
    }
}
复制代码

 

posted @   Jachs  阅读(303)  评论(0编辑  收藏  举报
编辑推荐:
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 没有源码,如何修改代码逻辑?
阅读排行:
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
点击右上角即可分享
微信分享提示
今天你要是不做,你明天还是得做。