第8章 数据库连接池

*数据库连接池的基本思想:为数据库建立一个缓冲池,预先在缓冲池中放入一定数量的连接,当需要建立数据库连接时,只需要从缓冲池中取出一个,使用完毕后再放回

*数据库连接池负责分配、管理和释放数据库连接,它语序应用程序重复使用一个现有的数据库连接,而不是重新建立一个。

1.C3P0数据库连接池

*导入jar包

*进入c3p0doc文档,查询配置方法

 *在src下创建c3p0-config.xml

<c3p0-config>
    <named-config name="helloC3P0">
        <!-- 提供获取连接的4个基本信息 -->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=UTC&amp;useSSL=false</property>
        <property name="user">root</property>
        <property name="password">123456</property>
        <!--进行数据库连接池管理的基本信息-->
        <property name="acquireIncrement">5</property><!--当数据库连接池中的连接数不够时,c3p0一次向向服务器申请的连接数-->
        <property name="initialPoolSize">10</property><!--c3p0初始化时的连接数-->
        <property name="minPoolSize">10</property><!--c3p0数据库连接池中维护最少的连接数-->
        <property name="maxPoolSize">100</property><!--c3p0数据库连接池中维护最多的连接数-->
        <property name="maxStatements">50</property><!--c3p0数据库连接池中维护的最多的Statement的个数-->
        <property name="maxStatementsPerConnection">5</property><!--c3p0数据库连接池中每个连接可以最多使用Statement的个数-->

    </named-config>
</c3p0-config>

*使用c3p0数据库连接池获取连接

    public void testGetConnection2() throws SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource("helloC3P0");
        Connection conn = cpds.getConnection();
        System.out.println(conn);
    }

2.DBCP数据库连接池

*导入jar包

*在src下创建dbcp.properties配置文件

driverClassNmae=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://@localhost:3306/test?serverTimezone=UTC&useSSL=false
username=root
password=168465

 *使用DBCP数据库连接池获取连接

public void testGetConnection2() throws Exception {
        Properties pros=new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("dbcp.properties");
        pros.load(is);
        DataSource source = BasicDataSourceFactory.createDataSource(pros);
        Connection conn = source.getConnection();
        System.out.println(conn);
    }

3.Druid数据库连接池

*导入jar包

*在src下创建druid.properties配置文件

username=root
password=321684635
url=jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=false&allowPublicKeyRetrieval=true&rewriteBatchedStatements=true
driverClassName=com.mysql.cj.jdbc.Driver

 *使用Druid数据库连接池获取连接

 public void getConnection() throws Exception {
        Properties pros = new Properties();
        InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
        pros.load(is);
        DataSource source = DruidDataSourceFactory.createDataSource(pros);
        Connection conn = source.getConnection();
        System.out.println(conn);
    }

*使用Druid的JDBCUtiles

    private static DataSource source;
    static {
        try {
            Properties pros=new Properties();
            InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
            pros.load(is);
            source= DruidDataSourceFactory.createDataSource(pros);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public static Connection getConnection() throws SQLException {
        return source.getConnection();
    }
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

桃桃tao

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值