欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

【JDBC】C3P0连接池的使用

程序员文章站 2022-07-01 13:14:01
C3P0连接池的c3p0 config.xml配置文件 工具类 单元测试 ......

c3p0连接池的c3p0-config.xml配置文件

<?xml version="1.0" encoding="utf-8"?>
<c3p0-config>
    <default-config>
        <property name="driverclass">com.mysql.cj.jdbc.driver</property>
        <property name="jdbcurl">jdbc:mysql:///jdbctest?servertimezone=hongkong</property>
        <property name="user">root</property>
        <property name="password">1234</property>
        <property name="initialpoolsize">5</property>
        <property name="maxpoolsize">20</property>
    </default-config>
</c3p0-config>

工具类

package jdbcutils;

import java.sql.connection;
import java.sql.resultset;
import java.sql.statement;

import com.mchange.v2.c3p0.combopooleddatasource;

public class jdbcutils {
    private static final combopooleddatasource cpds = new combopooleddatasource();
    
    public static connection getconection() throws exception {
        return cpds.getconnection();
    }
    
    public static void release(statement stmt, connection conn) {
        if(stmt != null) {
            try {
                stmt.close();
            }catch(exception e) {
                e.printstacktrace();
            }
            stmt = null;
        }
        
        if(conn != null) {
            try {
                conn.close();
            }catch(exception e) {
                e.printstacktrace();
            }
            conn = null;
        }       
    }
    
    public static void release(resultset rs, statement stmt, connection conn) {
        if(rs != null) {
            try {
                rs.close();
            }catch(exception e) {
                e.printstacktrace();
            }
            rs = null;
        }
        
        if(stmt != null) {
            try {
                stmt.close();
            }catch(exception e) {
                e.printstacktrace();
            }
            stmt = null;
        }
        
        if(conn != null) {
            try {
                conn.close();
            }catch(exception e) {
                e.printstacktrace();
            }
            conn = null;
        }       
    }
}

单元测试

package demo1;

import java.sql.connection;
import java.sql.preparedstatement;
import java.sql.resultset;

import org.junit.test;

import jdbcutils.jdbcutils;

public class jdbcdemo4 {
    
    @test
    public void demo4() {
        connection conn = null;
        preparedstatement pstmt = null;
        resultset rs = null;

        try {
            // 获得连接
            conn = jdbcutils.getconection();
            // 编写sql
            string sql = "select * from course";
            // 预编译sql
            pstmt = conn.preparestatement(sql);
            // 执行sql
            rs = pstmt.executequery();
            while (rs.next()) {
                system.out.println(rs.getint("id") + " " + rs.getstring("name") + " " + rs.getstring("category") + " "
                        + rs.getstring("desp") + " " + rs.gettimestamp("createtime"));
            }
        } catch (exception e) {
            e.printstacktrace();
        } finally {
            jdbcutils.release(rs, pstmt, conn);
        }
    }
}