• 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
  • 相关阅读:
    operator模块和functools模块
    函数注解
    用户定义的可调用类型、从定位参数到仅限关键字参数
    可调用对象
    nxos启动的初始化和https访问nx-api
    网络安全基础之网络协议与安全威胁
    华为AC中服务集命令解释配置
    转:图解ARP协议(四)代理ARP原理与实践(“善意的欺骗”)
    windows下python3 python2 共存下安装virtualenvwrapper
    关于网络安全学习的网站
  • 原文地址:https://www.cnblogs.com/zy-Luo/p/11600197.html
Copyright © 2020-2023  润新知