美文网首页
JavaMysql数据库连接池

JavaMysql数据库连接池

作者: chad_it | 来源:发表于2016-12-27 20:17 被阅读29次

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();

相关文章

网友评论

      本文标题:JavaMysql数据库连接池

      本文链接:https://www.haomeiwen.com/subject/nzxsmttx.html