c3p0基本使用方法和配置文件

基本使用方法

@Test
public void demo() throws PropertyVetoException, SQLException{
ComboPooledDataSource dataSource=new ComboPooledDataSource();
//设置四大参数的配置
dataSource.setDriverClass("com.mysql.jdbc.Driver");
dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/数据库的名称");
dataSource.setUser("root");
dataSource.setPassword("密码");

//对缓冲池进行设置
// dataSource.setAcquireIncrement(6);
// dataSource.setInitialPoolSize(10);
// dataSource.setMinPoolSize(5);
// dataSource.setMaxPoolSize(40);


Connection con=dataSource.getConnection();
//打印查看连接对象
System.out.println(con);//打印的结果为:com.mchange.v2.c3p0.impl.NewProxyConnection@5090d8ea
con.close();


配置文件

文件名必须叫 c3p0-config.xml

文件位置必须在src下

配置文件如下

<?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/数据库名称</property>

       <property name="user">root</property>

       <property name="password">密码</property>

       <property name="acquireIncrement">2</property>

       <property name="initialPoolSize">10</property>

       <property name="minPoolSize">2</property>

       <property name="maxPoolSize">10</property>

    </default-config>

//代码实现

public void test() throws SQLException{
ComboPooledDataSource dataSource=new ComboPooledDataSource();
Connection con=dataSource.getConnection();//直接得到该连接对象
System.out.println(con);//com.mchange.v2.c3p0.impl.NewProxyConnection@565bb966
con.close();
}


若在  < default-config >
后面加上 <named-config name="数据厂商-config">
代码
ComboPooledDataSource dataSource=new ComboPooledDataSource(“ 数据厂商-config”);//得到配置











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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值