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
  • 相关阅读:
    JS事件
    我刚知道的WAP app中meta的属性(转载)
    监控Tomcat解决方案(监控应用服务器系列文章分享)
    Java实现注册邮箱激活验证
    使用iScroll时,input等不能输入内容的解决方法(share)
    iScroll.js 用法参考 (share)
    Myeclipse常用快捷键
    ajax+json+Struts2实现list传递实例讲解
    JSTL的c:forEach标签(${status.index})
    JAVA导出pdf实例
  • 原文地址:https://www.cnblogs.com/zy-Luo/p/11600197.html
Copyright © 2011-2022 走看看