C3P0连接池

这里写图片描述

配置文件
这里写图片描述

一.未使用工具类

1.导入jar包
C3P0连接池_第1张图片

2.配置文件
这里写图片描述


<c3p0-config>

  <default-config>
    <property name="driverClass">com.mysql.jdbc.Driverproperty>
    <property name="jdbcUrl">jdbc:mysql:///web08property>
    <property name="user">rootproperty>
    <property name="password">ao1221property>
    <property name="initialPoolSize">5property>
    <property name="maxPoolSize">20property>
  default-config>

  <named-config name="itheima"> 
    <property name="driverClass">com.mysql.jdbc.Driverproperty>
    <property name="jdbcUrl">jdbc:mysql:///web08property>
    <property name="user">rootproperty>
    <property name="password">ao1221property>
  named-config>


c3p0-config>

3.测试

package cn.itheima.jdbc.test;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import cn.itheima.jdbc.utils.JDBCUtils_V3;

public class TestC3P0 {

    @Test
    public void testAddUser() {
        Connection conn=null;
        PreparedStatement pstmt = null;
        //1.创建自定义连接池对象
        ComboPooledDataSource dataSource = new ComboPooledDataSource();//加载默认的配置
        //ComboPooledDataSource dataSource = new ComboPooledDataSource("itheima");//加载有名称的配置

        try {
            //2.从池子中获取连接
            conn=dataSource.getConnection();
            String sql = "insert into tbl_user values(null,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "吕布2");
            pstmt.setString(2, "貂蝉2");
            int rows = pstmt.executeUpdate();
            if(rows>0) {
                System.out.println("添加成功!");
            }else {
                System.out.println("添加失败!");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils_V3.release(conn, pstmt, null);
        }
    }
}

运行结果:
C3P0连接池_第2张图片
C3P0连接池_第3张图片

二.使用工具类

1.编写工具类:

package cn.itheima.jdbc.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;
public class C3P0Utils {
    private static ComboPooledDataSource dataSource=new ComboPooledDataSource("itheima");
    public static DataSource getDataSource() {
        return dataSource;
    }

    public static Connection getConnection() {
        try {
            return dataSource.getConnection();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

}

2.测试

package cn.itheima.jdbc.test;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.junit.Test;

import com.mchange.v2.c3p0.ComboPooledDataSource;

import cn.itheima.jdbc.utils.C3P0Utils;
import cn.itheima.jdbc.utils.JDBCUtils_V3;

public class TestC3P0 {

    @Test
    public void testAddUser1() {
        Connection conn=null;
        PreparedStatement pstmt = null;

        try {
            //2.从池子中获取连接
            conn=C3P0Utils.getConnection();
            String sql = "insert into tbl_user values(null,?,?)";
            pstmt = conn.prepareStatement(sql);
            pstmt.setString(1, "吕布3");
            pstmt.setString(2, "貂蝉3");
            int rows = pstmt.executeUpdate();
            if(rows>0) {
                System.out.println("添加成功!");
            }else {
                System.out.println("添加失败!");
            }

        } catch (Exception e) {
            throw new RuntimeException(e);
        }finally {
            JDBCUtils_V3.release(conn, pstmt, null);
        }
    }
}

运行结果:
C3P0连接池_第4张图片
C3P0连接池_第5张图片

你可能感兴趣的:(MySql,Java必知必会)