zoukankan      html  css  js  c++  java
  • c3p0连接池和druid连接池的使用

    1.c3p0连接池

     没有配置文件的情况下

    @Test
        public void T1() throws SQLException, PropertyVetoException {
            ComboPooledDataSource cpds = new ComboPooledDataSource();
            cpds.setUser("root");
            cpds.setPassword("root");
            cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8");
            cpds.setDriverClass("com.mysql.jdbc.Driver");
    
            System.out.println(cpds.getConnection());  //获取连接
            cpds.close();
        }

     有配置文件的情况下

    @Test
        public void T1() throws SQLException, PropertyVetoException {
            // 在有配置文件c3p0-config.xml的情况下
            ComboPooledDataSource cpds = new ComboPooledDataSource("hellc3p0");  //此处的helloc3p0是配置文件中named-config的name
            Connection conn = cpds.getConnection();
            System.out.println(conn);
            conn.close();
            cpds.close();
    
            System.out.println(cpds.getConnection());  //获取连接
            cpds.close();
        }

     配置文件

    //文件名c3p0-config.xml
    <?
    xml version="1.0" encoding="UTF-8"?> <c3p0-config> <named-config name="helloc3p0"> <!-- 提供获取连接的4个基本信息 --> <property name="driverClass">com.mysql.jdbc.Driver</property> <property name="jdbcUrl">jdbc:mysql:///test</property> <property name="user">root</property> <property name="password">root</property> <!-- 进行数据库连接池管理的基本信息 --> <!-- 当数据库连接池中的连接数不够时,c3p0一次性向数据库服务器申请的连接数 --> <property name="acquireIncrement">5</property> <!-- c3p0数据库连接池中初始化时的连接数 --> <property name="initialPoolSize">10</property> <!-- c3p0数据库连接池维护的最少连接数 --> <property name="minPoolSize">10</property> <!-- c3p0数据库连接池维护的最多的连接数 --> <property name="maxPoolSize">100</property> <!-- c3p0数据库连接池最多维护的Statement的个数 --> <property name="maxStatements">50</property> <!-- 每个连接中可以最多使用的Statement的个数 --> <property name="maxStatementsPerConnection">2</property> </named-config> </c3p0-config>

    2.druid连接池

     没有配置文件的情况下

    public void T2() throws Exception {
            DruidDataSource dd = new DruidDataSource();
            dd.setUsername("root");
            dd.setPassword("2013.71123");
            dd.setUrl("jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8");
            dd.setDriverClassName("com.mysql.jdbc.Driver");
            
            System.out.println(dd.getConnection());   //获取连接
            dd.close();
        }

     有配置文件的情况下

    @Test
        public void T2() throws Exception {
            Properties properties = new Properties();
            InputStream in = new FileInputStream("source/jdbc.properties");
            properties.load(in);
            DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
            
            System.out.println(dataSource.getConnection());
            System.out.println(dataSource);
            in.close();
        }

     配置文件

    //文件名jbdc.properties
    url=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8 username=root password=2013.71123 className=com.mysql.jdbc.Driver
  • 相关阅读:
    A20 烧录和启动 log
    图像处理---图像分割技术---基于图像灰度分布的阈值方法一
    H.265---内容总览
    H.265---仿射运动模型和双线性运动模型
    H.265---帧内预测与帧间预测
    linux内核---进程通信---消息队列
    linux内核---嵌入式linux内核的五个子系统
    高并发服务器---nginx---实现负载均衡的几种方式
    高并发服务器---nginx---正向代理和反向代理
    【CV系列】基于形态学梯度的边缘检测
  • 原文地址:https://www.cnblogs.com/zy-Luo/p/11600197.html
Copyright © 2011-2022 走看看