zoukankan      html  css  js  c++  java
  • 数据库连接池

    一、c3p0

      1、引jar包:c3p0

      2、配置文件

    <?xml version="1.0" encoding="UTF-8"?>
    <c3p0-config>
        <default-config>
            <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://172.21.22.5:3306/studymysql</property>
            <property name="user">root</property>
            <property name="password">123456</property>
    
            <!--初始化连接数-->
            <property name="initialPoolSize">5</property>
            <!--连接超时时间-->
            <property name="checkoutTimeout">30000</property>
            <!--最大连接数-->
            <property name="maxPoolSize">10</property>
        </default-config>
    
        <named-config name="mySource">
            <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
            <property name="jdbcUrl">jdbc:mysql://172.21.22.5:3306/studymysql</property>
            <property name="user">root</property>
            <property name="password">123456</property>
    
            <property name="initialPoolSize">5</property>
            <property name="checkoutTimeout">30000</property>
            <property name="maxPoolSize">8</property>
        </named-config>
    </c3p0-config>

      3、代码

    public class PoolApplication {
        public static void main(String[] args) throws SQLException {
    /*
    1、c3p0连接池默认会读取src目录下的c3p0-config.xml或者c3p0.properties配置文件
    2、new ComboPooledDataSource(String):参数不传则读取配置文件中default-config的配置
        传named-config的name属性,则读取该named-config的配置
    3、Connection getConnection():获取连接
    */
            ComboPooledDataSource comboPooledDataSource = new ComboPooledDataSource("mySource");
    
            for (int i = 0; i < 10; i++) {
                Connection connection = comboPooledDataSource.getConnection();
                if (i == 0 || i == 1) {
                    connection.close();
                }
                System.out.println(connection);
            }
        }
    }

    二、druid

      1、引jar包:druid

      2、配置文件

    driverClassName=com.mysql.cj.jdbc.Driver
    url=jdbc:mysql://172.21.22.5:3306/studymysql
    username=root
    password=123456
    initialSize=5
    maxActive=10
    maxWait=30000

      3、代码

    public class DruidApplication {
        public static void main(String[] args) throws Exception {
    /*
    1、DataSource createDataSource(Properties):获取连接池
    2、Connection getConnection():获取连接
    */
            ClassLoader classLoader = DruidApplication.class.getClassLoader();
            InputStream inputStream = classLoader.getResourceAsStream("druid.properties");
            Properties properties = new Properties();
            properties.load(inputStream);
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
    
            Connection connection = dataSource.getConnection();
            System.out.println(connection);
        }
    }
  • 相关阅读:
    Creating a Simple Direct2D Application
    关于ssh加密方式的理解
    关于2147217913 从 char 数据类型到 datetime 数据类型的转换导致 datetime 值越界 的问题解决方法
    关于 win2003中ASP.Net 的edit configuration 无法使用的答疑
    vc 用ado访问Oracle数据库的代码示例
    手工移除vs6的VSS绑定
    关于:无法执行值从 char 到 char 的隐性转换,因为该值的排序规则因排序规则冲突而未能解决
    vs2003 无法进行调试的经历
    关于如何在VMware上安装Puppy Linux
    VB: DataGrid 的列可见问题
  • 原文地址:https://www.cnblogs.com/linding/p/13563387.html
Copyright © 2011-2022 走看看