Java c3p0和druid数据库连接池实现

导入配置

       <!--druid依赖-->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.1.20</version>
        </dependency>

        <!--c3p0依赖-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>c3p0</artifactId>
            <version>0.9.5.2</version>
        </dependency>
        <!--c3p0依赖的依赖-->
        <dependency>
            <groupId>com.mchange</groupId>
            <artifactId>mchange-commons-java</artifactId>
            <version>0.2.11</version>
        </dependency>

配置文件要求及配置

c3p0的配置文件必需命名为c3p0.properties或c3p0-config.xml,druid则没有命名要求,相应的c3p0放在resource下即可自动加载,而druid需手动加载。两者皆放在resource包下即可。
在这里插入图片描述

配置c3p0-config.xml

<c3p0-config>
    <!--默认配置-->
    <default-config>
        <!--驱动-->
        <property name = "driverClass">com.mysql.cj.jdbc.Driver</property>
        <!--url-->
        <property name = "jdbcUrl">jdbc:mysql://localhost:3306/test</property>
        <!--账号-->
        <property name = "user">root</property>
        <!--密码-->
        <property name = "password">root</property>
        <!--初始容量-->
        <property name = "initialPoolSize">5</property>
        <!--最大容量-->
        <property name = "maxPoolSize">10</property>
        <!--超时时间-->
        <property name = "checkoutTimeout">3000</property>
    </default-config>
    
    <!--指定配置-->
    <named-config name = "myc3p0config">
        <property name = "driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name = "jdbcUrl">jdbc:mysql://localhost:3306/test</property>
        <property name = "user">root</property>
        <property name = "password">root</property>

        <property name = "initialPoolSize">1</property>
        <property name = "maxPoolSize">5</property>
        <property name = "checkoutTimeout">3000</property>
    </named-config>
    
</c3p0-config>

druid配置

driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://loacalhost:3306/test
username=root
password=root
initialSize=5
maxActive=10
maxWait=3000

代码实现

c3p0实现

public class Main {

    public static void main(String[] args) {
        //创建默认配置连接池
        ComboPooledDataSource comboPooledDataSource1 = new ComboPooledDataSource();
        //创建指定配置连接池
        ComboPooledDataSource comboPooledDataSource2 = new ComboPooledDataSource("my-c3p0-config");
        //获取连接
        Connection connection1 = null;
        Connection connection2 = null;
        try {
            connection1 = comboPooledDataSource1.getConnection();
            connection2 = comboPooledDataSource2.getConnection();
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //返还给数据库连接池
            if (connection1 != null) {
                try {
                    connection1.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if (connection2 != null) {
                try {
                    connection2.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}

druid实现

public class Main {

    public static void main(String[] args) {
        Connection connection = null;
        try {
            //导入配置文件
            Properties properties = new Properties();
            //手动加载配置文件
            InputStream is = Main.class.getClassLoader().getResourceAsStream("druid.properties");
            properties.load(is);
            //创建指定配置数据库连接池
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            //获取连接
            connection = dataSource.getConnection();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (connection != null) {
                try {
                    //返还给数据库连接池
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值