• 第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. }
  • 相关阅读:
    RichView TRVStyle TextStyles
    C#结合JavaScript实现上传视频到腾讯云点播平台
    0基础了解电商系统如何对接支付渠道
    一文读懂,python实现常用的数据编码和对称加密
    iMazing2024年最新许可证-iMazing许可证激活补丁
    2022年湖北劳务资质办理需要准备什么资料?办理劳务需要注意哪些呢?甘建二
    XXE 漏洞及案例实战
    javaScript深拷贝和浅拷贝简单梳理
    QT万年历
    鲸鱼优化算法(WhaleOptimizationAlgorithm,WOA)附matlab代码
  • 原文地址:https://blog.csdn.net/weixin_47687315/article/details/127886025