C3P0是一款开源免费的连接池,连接池的主要作用就是提高性能。
使用方法:

  1. 导入jar包:c3p0的jar包和mysql的jar包(小编使用的是mysql数据库),这两个jar包可以自行百度下载。也可以给小编留言哦。
  2. 编写配置文件c3p0-config.xml

    • 配置文件的名称必须为:c3p0-config.xml;
    • 一般情况放在src目录里面。
    • 文件的配置(以下是主要配置,其他选项可以参考手册)

      <?xml version="1.0" encoding="UTF-8"?>
      <c3p0-config>
       <default-config>
      <!-- 连接数据库的参数 -->
      <property name="driverClass">com.mysql.jdbc.Driver</property>
      <property name="jdbcUrl">jdbc:mysql:///ning</property>
      <property name="user">root</property>
      <property name="password">root</property>
      <!-- 初始化连接数 -->
      <property name="initialPoolSize">5</property>
      <!-- 最大连接数 -->
      <property name="maxPoolSize">20</property>
       </default-config>
       <named-config name="ning">
      <property name="driverClass">com.mysql.jdbc.Driver</property>
      <property name="jdbcUrl">jdbc:mysql:///ning</property>
      <property name="user">root</property>
      <property name="password">root</property>
       </named-config>
      </c3p0-config>
  3. 工具类

    package com.sunxiaoning.jdbc.utils;
    
    import java.sql.Connection;
    import java.sql.SQLException;
    
    import com.mchange.v2.c3p0.ComboPooledDataSource;
    
    public class C3P0Utils {
     private static ComboPooledDataSource dataSource = new ComboPooledDataSource("itheima");
     
     /**
      * 获得数据源(连接池)
      * @return
      */
     public static ComboPooledDataSource getDataSource() {
         return dataSource;
     }
     
     /**
      * 获得连接
      * @return
      */
     public static Connection getConnection() {
         try {
             return dataSource.getConnection();
         } catch (SQLException e) {
             throw new RuntimeException();
         }
     }
     
     /**
      * 归还连接
      * @param conn
      */
     public static void closeConn(Connection conn){
         try {
             if(conn!=null && conn.isClosed()){
                 conn.close();
             }
         } catch (SQLException e) {
             e.printStackTrace();
         }
     }
    }
    

    附录:测试类

    package com.sunxiaoning.jdbc.test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    import org.junit.jupiter.api.Test;
    
    import com.sunxiaoning.jdbc.utils.C3P0Utils;
    /**
     * 测试C3P0
     * @author sunxiaoning
     *
     */
    package com.sunxiaoning.jdbc.test;
    
    import java.sql.Connection;
    import java.sql.PreparedStatement;
    import java.sql.SQLException;
    
    import org.junit.jupiter.api.Test;
    
    import com.sunxiaoning.jdbc.utils.C3P0Utils;
    /**
     * 测试C3P0
     * @author sunxiaoning
     *
     */
    public class TestC3P0 {
    
     @Test
     public void testAddUser() {
         Connection conn = null;
         PreparedStatement pstmt = null;
         
         try {
             conn = C3P0Utils.getConnection();
             String sql = "insert into users values(null,?,?)";
             pstmt = conn.prepareStatement(sql);
             pstmt.setString(1, "小宁");
             pstmt.setString(2, "qwe");
             int row = pstmt.executeUpdate();
             if(row>0) {
                 System.out.println("添加成功");
             }else {
                 System.out.println("添加失败");
             }
         } catch (SQLException e) {
             throw new RuntimeException();
         }finally {
             C3P0Utils.closeConn(conn);
             if (pstmt!=null) {
                 try {
                     pstmt.close();
                 } catch (SQLException e) {
                     e.printStackTrace();
                 }
             }
         }
         
     }
    }
    
Last modification:February 15, 2019
If you think my article is useful to you, please feel free to appreciate