JavaMysql数据库连接池

2016-12-27  本文已影响29人  chad_it

C3P0

  1. 导入类库
    数据库连接池C3P0 链接: https://pan.baidu.com/s/1coYMVc 密码: 4e4h
    数据库连接 链接: https://pan.baidu.com/s/1crGEUq 密码: d7pa
  2. 在src下创建配置文件 c3p0-config.xml
    更多配置属性可以查看
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/javawu</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
    </default-config>
    
    <name-config name="test">
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/javawu_homework</property>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="user">root</property>
        <property name="password">root</property>
        <property name="initialPoolSize">3</property>
        <property name="maxPoolSize">6</property>
    </name-config>
</c3p0-config>
  1. 创建连接池的核心类
//自动加载src下的c3p0-config.xml文件中的default-config
ComboPooledDataSource ds = new ComboPooledDataSource();
  1. 通过连接池获取连接
Connection connection = ds.getConnection();
  1. 关闭连接
connection.close();
上一篇 下一篇

猜你喜欢

热点阅读