• 第8章 数据库连接池


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

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

    1.C3P0数据库连接池

    *导入jar包

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

     *在src下创建c3p0-config.xml

    1. <c3p0-config>
    2. <named-config name="helloC3P0">
    3. <property name="driverClass">com.mysql.cj.jdbc.Driverproperty>
    4. <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?serverTimezone=UTC&useSSL=falseproperty>
    5. <property name="user">rootproperty>
    6. <property name="password">123456property>
    7. <property name="acquireIncrement">5property>
    8. <property name="initialPoolSize">10property>
    9. <property name="minPoolSize">10property>
    10. <property name="maxPoolSize">100property>
    11. <property name="maxStatements">50property>
    12. <property name="maxStatementsPerConnection">5property>
    13. named-config>
    14. c3p0-config>

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

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

    2.DBCP数据库连接池

    *导入jar包

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

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

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

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

    3.Druid数据库连接池

    *导入jar包

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

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

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

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

    *使用Druid的JDBCUtiles

    1. private static DataSource source;
    2. static {
    3. try {
    4. Properties pros=new Properties();
    5. InputStream is = ClassLoader.getSystemClassLoader().getResourceAsStream("druid.properties");
    6. pros.load(is);
    7. source= DruidDataSourceFactory.createDataSource(pros);
    8. } catch (Exception e) {
    9. e.printStackTrace();
    10. }
    11. }
    12. public static Connection getConnection() throws SQLException {
    13. return source.getConnection();
    14. }
  • 相关阅读:
    【python打包】使用pyinstaller对python程序进行打包
    Mybatis的生命周期和作用域
    Pycharm和Jupyter的对比
    elementui 表单规则
    SpringCloud的新闻资讯项目01--- 环境搭建、SpringCloud微服务(注册发现、服务调用、网关
    基于Html+Css+javascript的游戏网页制作(游戏主题)-英雄联盟6页
    datadog ebpf模块 offset-guess.o 问题排查解决
    神经元网络
    基于Java的药品商城管理系统设计与实现(源码+lw+部署文档+讲解等)
    在KubeSphere启用基于Jenkins的DevOps
  • 原文地址:https://blog.csdn.net/weixin_47687315/article/details/127886025