c3p0连接池和druid连接池的使用

故事扮演 提交于 2019-11-30 15:05:17

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.propertiesurl=jdbc:mysql://localhost:3306/test?characterEncoding=UTF-8
username=root
password=2013.71123
className=com.mysql.jdbc.Driver

 

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!