连接池c3p0

连接池c3p0

C3P0:hibernate和spring使用,有自动回收空闲连接的功能.

使用步骤:
  1.导入jar包(c3p0-0.9.1.2.jar)
  2.使用api


a.硬编码(不推荐)
  new ComboPooledDataSource()

 

步骤:

  1.创建一个DataSourse项目

  2.新建一个包com.c3p0.hjh

  3.编写一个类C3p0Test01.java

  4.导包,lib下放入c3p0-0.9.2.1.jar和mchange-commons-java-0.2.3.4.jar;导完jar记得buildpath一下

 

 代码执行前数据库数据:

 

 代码执行后数据库数据:

 

 JDBCUtil.java工具类源码:

package com.util.hjh;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class JDBCUtil {
    final static String driver = "com.mysql.jdbc.Driver";
    final static String url = "jdbc:mysql://localhost/hejh?useUnicode=true&characterEncoding=UTF-8";
    final static String user  = "root";
    final static String password = "root";
    
    Connection conn = null;
    PreparedStatement ps = null;
    Statement st = null;
    ResultSet rs = null;
        
    /**获取连接*/
    public static Connection getConnection() throws SQLException  {
        Connection    conn = null;
        try {
            //注册驱动
            Class.forName(driver);
            //获取连接
            conn = DriverManager.getConnection(url, user, password);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }                    
        return conn;    
    }
    
    /**关闭资源closeResourse(conn,st)*/
    public static void closeResourse(Connection conn,Statement st) {
        try {
            if(st!=null) {
                st.close();
            }else {
                st = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null) {
                conn.close();
            }else {
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**关闭资源closeResourse(conn,ps)*/
    public static void closeResourse(Connection conn,PreparedStatement ps) {
        try {
            if(ps!=null) {
                ps.close();
            }else {
                ps = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        try {
            if(conn!=null) {
                conn.close();
            }else {
                conn = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    /**关闭资源closeResourse(rs)*/
    public static void closeResourse(ResultSet rs) {
        try {
            if(rs!=null) {
                rs.close();
            }else {
                rs = null;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

 

 C3p0Test01.java源码:

package com.c3p0.hjh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.util.hjh.JDBCUtil;

/*
 * a.硬编码(不推荐)
        new ComboPooledDataSource()
 * */
public class C3p0Test01 {
    @Test
    public void test() throws Exception {
        //建立连接池
        ComboPooledDataSource ds = new ComboPooledDataSource();
        
        //设置基本参数
        ds.setDriverClass("com.mysql.jdbc.Driver");
        ds.setJdbcUrl("jdbc:mysql://localhost/jdbc");
        ds.setUser("root");
        ds.setPassword("root");
        
        //操作数据库数据
        Connection conn = ds.getConnection();
        String sql = "update student set id =? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, 06);
        ps.setInt(2, 10);
        int i = ps.executeUpdate();
        System.out.println("更新了"+i+"行数据");
        
        //关闭资源
        JDBCUtil.closeResourse(conn, ps);     
    }   
}

 

 


b.配置文件
  配置文件的名称:c3p0.properties 或者 c3p0-config.xml
  配置文件的路径:src下

  编码只需要一句话
  new ComboPooledDataSource()//使用默认的配置
  new ComboPooledDataSource(String configName)//使用命名的配置 若配置的名字找不到,使用默认的配置

 

 

步骤:

  1.创建一个DataSourse项目

  2.新建一个包com.c3p0.hjh

  3.编写一个类C3p0Test2.java

  4.导包,lib下放入c3p0-0.9.2.1.jar和mchange-commons-java-0.2.3.4.jar;导完jar记得buildpath一下

  5.编写配置文件c3p0.properties

 

 c3p0.properties文件内容:

c3p0.driverClass=com.mysql.jdbc.Driver
c3p0.jdbcUrl=jdbc:mysql://localhost/jdbc
c3p0.user=root
c3p0.password=root

 

 

 代码运行前数据库数据显示如下:

 

 代码运行后数据库数据显示如下:

 

  JDBCUtil.java工具类源码同上,略

 

C3p0Test2.java源码如下:

package com.c3p0.hjh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.util.hjh.JDBCUtil;

public class C3p0Test2 {
    @Test
    public void test() throws Exception {
        //建立连接池
        ComboPooledDataSource ds = new ComboPooledDataSource();
        
        //操作数据库数据
        Connection conn = ds.getConnection();
        String sql = "update student set name =? where id=?";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setString(1,"haha");
        ps.setInt(2, 3);
        int i = ps.executeUpdate();
        System.out.println("更新了"+i+"行数据");
        
        //关闭资源
        JDBCUtil.closeResourse(conn, ps);
    }
}

 

 

 使用c3p0-config.xml配置文件

 

 代码执行之前的数据库数据:

 

 代码执行之后数据库中的数据:

 

c3p0-config.xml配置文件内容:

<c3p0-config>
    <!-- 默认配置,如果没有指定则使用这个配置 -->
    <default-config>
        <!-- 基本配置 -->
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://localhost/jdbc</property>
        <property name="user">root</property>
        <property name="password">root</property>
    
        <!--扩展配置-->
        <property name="checkoutTimeout">30000</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> 
    
    
    <!-- 命名的配置 -->
    <named-config name="XXX">
        <property name="driverClass">com.mysql.jdbc.Driver</property>
        <property name="jdbcUrl">jdbc:mysql://127.0.0.1:3306/xxxx</property>
        <property name="user">root</property>
        <property name="password">1234</property>
        
        
        <!-- 如果池中数据连接不够时一次增长多少个 -->
        <property name="acquireIncrement">5</property>
        <property name="initialPoolSize">20</property>
        <property name="minPoolSize">10</property>
        <property name="maxPoolSize">40</property>
        <property name="maxStatements">20</property>
        <property name="maxStatementsPerConnection">5</property>
    </named-config>
</c3p0-config> 

 

 C3p0Test03.java源码:

package com.c3p0.hjh;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.util.hjh.JDBCUtil;

public class C3p0Test03 {
    @Test
    public void test() throws Exception {
        //建立连接池
        ComboPooledDataSource ds = new ComboPooledDataSource();
        
        //操作数据库数据
        Connection conn = ds.getConnection();
        String sql = "insert into student values(?,?,?)";
        PreparedStatement ps = conn.prepareStatement(sql);
        ps.setInt(1, 22);
        ps.setString(2,"erya");
        ps.setInt(3, 103);
        int i = ps.executeUpdate();
        System.out.println("插入了"+i+"行数据");
        
        //关闭资源
        JDBCUtil.closeResourse(conn, ps);
    }   
}
posted @ 2019-05-15 16:09  雪顶儿  阅读(193)  评论(0编辑  收藏  举报