c3p0使用

简介:

C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展

使用步骤:

一、导包:

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.47</version>
</dependency>

<!-- https://mvnrepository.com/artifact/com.mchange/c3p0 -->
<dependency>
    <groupId>com.mchange</groupId>
    <artifactId>c3p0</artifactId>
    <version>0.9.5.5</version>
</dependency>

方法一:set方法配置c3p0

public class C3P0ConfigDataSource {
    public static ComboPooledDataSource dataSource = new ComboPooledDataSource();

    // 使用set方法配置C3P0
    public static void configDataSource(){
        try {
            dataSource.setDriverClass("com.mysql.jdbc.Driver");
            dataSource.setJdbcUrl("jdbc:mysql://localhost:3308/jsp?useUnicode=true&characterEncoding=utf8&useSSL=false");
            dataSource.setUser("root");
            dataSource.setPassword("123456");
            // 连接初始化时创建的连接数
            dataSource.setInitialPoolSize(3);
            // 连接池中拥有的最大连接数,如果获得新的连接时,连接总数已满,则不会再获取新连接,而是等待其他连接释放
            dataSource.setMaxPoolSize(10);
            // 连接池保持的最小连接数
            dataSource.setMinPoolSize(3);
            // 连接池在无空闲连接可用时一次性创建的新数据库连接数
            dataSource.setAcquireIncrement(3);
        } catch (PropertyVetoException e) {
            e.printStackTrace();
        }
    }
    // 获取连接
    public static Connection getConnection(){
        Connection connection = null;
        configDataSource();
        try {
            connection = dataSource.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return connection;
    }
}

方法二:配置文件使用

  1. 在src目录下任意地方创建c3p0-config.xml配置文件,(在maven中,一般放在resources文件夹下)

  2. 编写配置信息

    <?xml version="1.0" encoding="UTF-8" ?>
    <c3p0-config>
    <!--  默认配置  -->
        <default-config>
            <property name="user">root</property>
            <property name="password">123456</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3308/jsp?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
            <property name="checkoutTimeout">30000</property>
            <property name="idleConnectionTestPeriod">30</property>
            <property name="initialPoolSize">3</property>
            <property name="maxIdleTime">30</property>
            <property name="maxPoolSize">100</property>
            <property name="minPoolSize">2</property>
            <property name="maxStatements">200</property>
        </default-config>
    
        <named-config name="test">
            <property name="user">root</property>
            <property name="password">123456</property>
            <property name="jdbcUrl">jdbc:mysql://localhost:3308/jsp?useUnicode=true&amp;characterEncoding=utf8&amp;useSSL=false</property>
            <property name="driverClass">com.mysql.jdbc.Driver</property>
        </named-config>
    
    
    </c3p0-config>
    
  3. 代码使用

    public class C3P0ConfigDataSource2 {
        // 使用默认配置连接
        public static ComboPooledDataSource dataSource = new ComboPooledDataSource();
        // 使用指定名连接
        public static ComboPooledDataSource dataSource2 = new ComboPooledDataSource("test");
    
    
        public static Connection getConnection(){
            Connection connection = null;
            try {
                connection = dataSource.getConnection();
            } catch (SQLException e) {
                e.printStackTrace();
            }
            return connection;
        }
    
        public static void main(String[] args) {
            System.out.println(getConnection());
        }
    
    }
    
posted @   yaya_sama  阅读(120)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 【自荐】一款简洁、开源的在线白板工具 Drawnix
· 没有Manus邀请码?试试免邀请码的MGX或者开源的OpenManus吧
· 园子的第一款AI主题卫衣上架——"HELLO! HOW CAN I ASSIST YOU TODAY
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
点击右上角即可分享
微信分享提示