数据库连接池C3P0连接(附jar包)

1.创建ComboPooledDataSource对象连接**

user=root
password=123
url=jdbc:mysql://localhost:3306/sql?rewriteBatchedStatements=true
driver=com.mysql.cj.jdbc.Driver
    @Test
    public void testC3P0() throws IOException, PropertyVetoException, SQLException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource();
        Properties properties = new Properties();
        properties.load(new FileInputStream("src\\sql.properties"));
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String url = properties.getProperty("url");
        String driver = properties.getProperty("driver");

        //给数据源comboPooledDataSource设置相关参数
        comboPooledDataSource.setDriverClass(driver);
        comboPooledDataSource.setJdbcUrl(url);
        comboPooledDataSource.setUser(user);
        comboPooledDataSource.setPassword(password);

        //设置初始化连接数
        comboPooledDataSource.setInitialPoolSize(10);
        //最大连接数
        comboPooledDataSource.setMaxPoolSize(50);
        Connection connection = comboPooledDataSource.getConnection();

        System.out.println("连接成功");

        connection.close();
    }

2.配置文件c3p0-config.xml连接

<c3p0-config>

    <named-config name="c3p0_test">
        <!-- 驱动类 -->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <!-- url-->
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/sql?rewriteBatchedStatements=true</property>
        <!-- 用户名 -->
        <property name="user">root</property>
        <!-- 密码 -->
        <property name="password">123</property>
        <!-- 每次增长的连接数-->
        <property name="acquireIncrement">5</property>
        <!-- 初始的连接数 -->
        <property name="initialPoolSize">10</property>
        <!-- 最小连接数 -->
        <property name="minPoolSize">5</property>
        <!-- 最大连接数 -->
        <property name="maxPoolSize">10</property>

        <!-- 可连接的最多的命令对象数 -->
        <property name="maxStatements">5</property>

        <!-- 每个连接对象可连接的最多的命令对象数 -->
        <property name="maxStatementsPerConnection">2</property>
    </named-config>
</c3p0-config>

 @Test
    public void testC3P01() throws SQLException {
        ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("c3p0_test");

        Connection connection = comboPooledDataSource.getConnection();

        System.out.println("连接成功");
        connection.close();

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值