码迷,mamicode.com
首页 > 数据库 > 详细

【JDBC】C3P0连接池的使用

时间:2019-10-06 20:37:53      阅读:129      评论:0      收藏:0      [点我收藏+]

标签:ret   util   utils   resultset   tst   xml配置   curl   course   连接   

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);
        }
    }
}

【JDBC】C3P0连接池的使用

标签:ret   util   utils   resultset   tst   xml配置   curl   course   连接   

原文地址:https://www.cnblogs.com/huowuyan/p/11628092.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!