JDBC的C3P0的连接方式

一、C3P0

c3p0是一个开源的JDBC连接池,它相对于DBCP和Druid来说有一个更方便的配置连接方式,就是自动读取配置文件。

二、C3P0的连接方式一

jar包下载地址:https://mvnrepository.com/artifact/c3p0/c3p0/0.9.1.2

package com.jun.jdbc.datasource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import java.io.FileInputStream;
import java.sql.Connection;
import java.util.Properties;

public class C3P001 {
    public static void main(String[] args) throws Exception {
        C3P001 c3P001 = new C3P001();
        c3P001.testC3P0_01();
    }
    //方式1:相关参数,在程序中指定user,url,password等

    public void testC3P0_01() throws Exception {
        //创建一个数据源对象
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        //通过配置文件mysql.properties获取相关连接的信息
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\mysql.properties"));
        //读取相关属性
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");
        //给数据源comboPooledDataSource设置相关参数
        //连接管理是由comboPooledDataSource来管理
        comboPooledDataSource.setDriverClass(driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);
        //设置初始化连接数
        comboPooledDataSource.setInitialPoolSize(5);
        //最大连接数
        comboPooledDataSource.setMaxPoolSize(50);
        long start = System.currentTimeMillis();
        for (int i = 0; i <5000 ; i++) {
            Connection connection = comboPooledDataSource.getConnection();//由DataSuore接口实现
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("C3P0 5000次连接耗时="+(end-start));
    }
}

三、C3P0的连接方式二

 c3p0-config.xml配置文件

<c3p0-config>
    <!--数据库名称代表连接池-->
    <named-config name="jun">
        <!--驱动类-->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <!--url -->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/jun_db02</property>
        <!--用户名-->
        <property name="user">root</property>
        <!--密码-->
        <property name="password">654321</property>
        <!--每次增长连接数-->
        <property name="acquireIncrement">5</property>
        <!--初始连接数-->
        <property name="initialPoolSize">10</property>
        <!--最小连接数-->
        <property name="minPoolSize">5</property>
        <!--最大连接数-->
        <property name="maxPoolSize">50</property>
        <!--可连接的最多的命令对象数-->
        <property name="maxStatements">5</property>
        <!--每个连接对象可连接的最多的命令对象数-->
        <property name="maxStatementsPerConnection">2</property>
    </named-config>
</c3p0-config>

 

package com.jun.jdbc.datasource;


import com.mchange.v2.c3p0.ComboPooledDataSource;
import org.junit.Test;

import java.sql.Connection;
import java.sql.SQLException;

public class C3P002 {

    //方式2:使用配置文件模板来完成
    //将c3p0提供的c3p0.config.xml考到src目录下
    //这个文件指定了连接数据库和连接池的相关参数
    @Test
    public void testC3P0_02() throws SQLException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("jun");
        long start = System.currentTimeMillis();
        System.out.println("计时开始...");
        for (int i = 0; i <5000 ; i++) {
        Connection connection = comboPooledDataSource.getConnection();
//        System.out.println("连接成功");
            connection.close();
        }
        long end = System.currentTimeMillis();
        System.out.println("c3p0连接方式二耗时="+(end-start));//c3p0连接方式二耗时=481
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

鸭鸭老板

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值