测试c3p0数据连接池的使用----简单笔记

加入jar包
上代码


配置文件


<c3p0-config>
  <!-- This app is massive! -->
  <named-config name="myconfig"> 

    <!-- 设置常用的属性  url  用户 密码 驱动  还是set后小写第一个字母 -->
    <property name="driverClass">com.mysql.jdbc.Driver</property>
    <property name="jdbcUrl">jdbc:mysql://localhost/student?useSSL=false</property>
    <property name="user">root</property>
    <!--    <property name="password">50</property> -->>


    <!--  初始化连接池 一次增加的数量, 初始化数量 最小的连接数 最大的连接数  -->>
    <property name="acquireIncrement">5</property>
    <property name="initialPoolSize">10</property>
    <property name="minPoolSize">5</property>
    <property name="maxPoolSize">50</property>

    <!-- 连接池维护的 statement数量 和每个链接最大使用的statement的数量 -->
    <property name="maxStatements">20</property> 
    <property name="maxStatementsPerConnection">5</property>

  </named-config>
</c3p0-config>
package top.demo.test;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;

import com.mchange.v2.c3p0.*;

public class TestC3p0 {

    public static void main(String argv[]) throws PropertyVetoException, SQLException {

        test2();


    }


    public static void test1() throws PropertyVetoException, SQLException{

        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver            
        cpds.setJdbcUrl( "jdbc:mysql://localhost/student?useSSL=false" );
        cpds.setUser("root");                                  
        cpds.setPassword("");    

        //设置池 初始化大小
        cpds.setInitialPoolSize(5);
        Connection con =cpds.getConnection();


        System.out.println(con);

    }

    //c3p0可以使用配置文件加载配置 但是推荐使用xml加载配置
    public static void test2() throws SQLException {
        //在src下建立c3p0-config.xml 文件 文件名必须这样写
        //有了xml配置文件 new 的使用写 在  <named-config name="myconfig">  填写的name即可
        ComboPooledDataSource dataSources= new ComboPooledDataSource("myconfig");
        Connection con =dataSources.getConnection();
        System.out.println(con);
    }


}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值