C3P0Utils工具类的使用

准备工作:使用c3p0需要用到的jar包有:

c3p0-0.9.1.2.jar
commons-logging-1.2.jar
mchange-commons-java-0.2.20.jar
mysql-connector-java-5.1.7-bin.jar

//以下jar包是在使用JdbcTemplate模板时需要
spring-beans-5.2.6.RELEASE.jar
spring-core-5.2.6.RELEASE.jar
spring-jdbc-5.2.6.RELEASE.jar
spring-tx-5.2.6.RELEASE.jar

推荐maven仓库下载
csdn打包下载:c3p0连接池需要的jar包,C3P0Utils.java,c3p0-config.xml

一、在src目录下创建一个名为c3p0-config.xml的文件,如下:

<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
    <default-config>
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost:3306/poem</property>
        <!--<property name="jdbcUrl">jdbc:mysql://localhost:3306/poem?useUnicode=true&amp;characterEncoding=UTF-8</property>-->
        <property name="user">root</property>
        <property name="password">root</property>

        <property name="checkoutTimeout">1000</property>
        <property name="idleConnectionTestPeriod">30</property>
        <property name="initialPoolSize">10</property>
        <property name="maxIdleTime">30</property>
        <property name="maxPoolSize">100</property>
        <property name="minPoolSize">10</property>
        <property name="maxStatements">200</property>
    </default-config>
</c3p0-config>

二、C3P0Utils工具类

package com.hebin.util;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * c3p0工具类(数据库连接池工具),用于管理多个数据库连接对象。
 */
public class C3P0Utils {
    
    

    private static ComboPooledDataSource ds = null;

    static {
    
    
        // 新建数据库连接池,注意这里的参数必须同c3p0-config.xml中的named-config标签中的name的值相同
        // 如果不写,则默认调用default-config
        ds = new ComboPooledDataSource();
    }

    public static DataSource getDataSource() {
    
    
        return ds;
    }

    /**
     * 得到数据库连接对象
     * @return
     */
    public static Connection getConnection() {
    
    
        Connection conn;
        try {
    
    
            conn = ds.getConnection();
            return conn;
        } catch (SQLException e) {
    
    
            throw new RuntimeException("服务器忙");
        }
    }

    /**
     * 关闭所有资源连接
     * @param conn
     * @param ps
     * @param rs
     */
    public static void releaseAll(Connection conn, Statement ps, ResultSet rs) {
    
    
        if (conn != null) {
    
    
            try {
    
    
                conn.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
            conn = null;
        }

        if (ps != null) {
    
    
            try {
    
    
                ps.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
            ps = null;
        }

        if (rs != null) {
    
    
            try {
    
    
                rs.close();
            } catch (SQLException e) {
    
    
                e.printStackTrace();
            }
            rs = null;
        }
    }

}

三、使用JdbcTemplate模板测试

 @Test
 public void C3P0Test(){
    
    
     Poem poem = new Poem();
     poem.setTitle("静夜思");
     poem.setDynasty("唐");
     poem.setAuthor("李白");
     poem.setContent("床前明月光,疑是地上霜。巨头网映月,低头是故乡。");
     poem.setCategory("唐诗三百首");

     System.out.println(poem.toString());

     // 初始化JdbcTemplate模板
     JdbcTemplate jdbcTemplate = new JdbcTemplate(C3P0Utils.getDataSource());
     String sql = "insert into poem values(null,'"+poem.getTitle()+"','"+poem.getDynasty()+"','"+poem.getAuthor()+"','"+poem.getContent()+"','"+poem.getCategory()+"')";
     jdbcTemplate.execute(sql);

 }

四、不使用spring系列的jar包进行测试,请转场
数据库连接池【C3P0、阿里Druid、Spring JDBC】

猜你喜欢

转载自blog.csdn.net/weixin_44505194/article/details/108444725