数据库连接池 c3p0数据库连接池的两种实现方式及在JDBCUtils工具类中实现

1.两种实现方式
需要在src目录下创建c3p0-config.xml文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>


    <named-config name="helloc3p0">
        <!--提供获取连接的4个基本信息-->
        <property name="driverClass">com.mysql.cj.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/test?characterEncoding=utf8&amp;useSSL=false&amp;serverTimezone=UTC&amp;rewriteBatchedStatements=true</property>
        <property name="user">root</property>
        <property name="password">a421546457</property>

        <!--进行数据库连接池管理的基本信息-->
        <!--当数据库连接池中的连接数不够时,c3p0一次性向数据库申请的连接数-->
        <property name="acquireIncrement">5</property>
        <!--初始时数据库连接池中的连接数-->
        <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>

测试两种实现方式

public class C3P0Test {
    @Test
    public void testGetConnection() throws PropertyVetoException, SQLException {

        /*
        * 方式一
        * */
        //获取c3p0数据库连接池
        ComboPooledDataSource cpds = new ComboPooledDataSource();
        cpds.setDriverClass( "com.mysql.cj.jdbc.Driver" ); //loads the jdbc driver
        cpds.setJdbcUrl( "jdbc:mysql://localhost:3306/test?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true" );
        cpds.setUser("root");
        cpds.setPassword("a421546457");

        //通过设置相关的参数,对数据库连接池进行管理
        //设置初始时数据库连接池中的连接数
        cpds.setInitialPoolSize(10);
        Connection conn = cpds.getConnection();
        System.out.println(conn);

        //销毁c3p0数据库连接池
        //DataSources.destroy(cpds);
    }

    /*
    * 方式二:使用配置文件
    * */
    @Test
    public void testGetConnection1() throws SQLException {
        ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");
        Connection conn = cpds.getConnection();
        System.out.println(conn);
    }
}

2.JDBCUtils工具类

    /*
    * 使用c3p0的数据库连接池技术
    * */
    //数据库连接池只需要一个
    private static ComboPooledDataSource cpds = new ComboPooledDataSource("helloc3p0");

    public static Connection getConnection1() throws SQLException {
        Connection conn = cpds.getConnection();
        return conn;
    }

    //获取数据库连接
    public static Connection getConnection() throws Exception {
        //1.读取配置文件中的四个基本信息
        Properties properties = new Properties();
        //通过类加载器获取对应文件的输入流(默认是在src的文件)
        properties.load(ClassLoader.getSystemClassLoader().getResourceAsStream("jdbc.properties"));
        String url = properties.getProperty("url");
        String user = properties.getProperty("user");
        String password = properties.getProperty("password");
        String driverClass = properties.getProperty("driverClass");

        //2.加载数据库驱动
        Class.forName(driverClass);

        //3.获取数据库连接
        Connection conn = DriverManager.getConnection(url,user,password);
        return conn;

    }

    //关闭资源
    public static void close(Connection conn, Statement stmt){
        try {
            if (conn!=null)
                conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (stmt!=null)
                stmt.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

    /*
     * 关闭资源
     * */
    public static void close(Connection conn, Statement stmt, ResultSet rs){
        try {
            if (conn!=null)
                conn.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (stmt!=null)
                stmt.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        try {
            if (rs!=null)
                rs.close();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值