天天看点

C3P0连接池1.导入所需的jar文件2.项目目录结构

目录

1.导入所需的jar文件

2.项目目录结构

c3p0-config.xml

C3P0Util.java

C3P0Demo.java

结果

数据库

1.导入所需的jar文件

c3p0-0.9.5.2.jar

machange-commons-java-0.2.11.jar

前两个下载方式:https://blog.csdn.net/qq_32965187/article/details/86147216

mysql-connector-java-8.0.12.jar

下载方式:https://blog.csdn.net/qq_32965187/article/details/83141405

2.项目目录结构

C3P0连接池1.导入所需的jar文件2.项目目录结构

c3p0-config.xml

c3p0的配置文件,一定要用这个名字。

注意url的&要写成&

<?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://localhost:3306/bank?useSSL=false&amp;serverTimezone=UTC</property>
    <property name="user">root</property>
    <property name="password">123456</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>
           

C3P0Util.java

package com.zhujunwei.util ;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mchange.v2.c3p0.ComboPooledDataSource;


public class C3P0Util {

	static ComboPooledDataSource dataSource = null ;
	static {
		dataSource = new ComboPooledDataSource() ;
	}
	/**
	 * 得到Connection连接
	 * @return Connection
	 * @throws SQLException 
	 */
	public  static Connection getConnection() throws SQLException {
		return dataSource.getConnection();
		
	}
	
	/**
	 * 释放资源
	 * @param conn
	 * @param st
	 * @param rs
	 */
	public static void release(Connection conn , Statement st , ResultSet rs){
		closeRs(rs);
		closeSt(st);
		closeConn(conn);
	}
	public static void release(Connection conn , Statement st){
		closeSt(st);
		closeConn(conn);
	}

	
	private static void closeRs(ResultSet rs){
		try {
			if(rs != null){
				rs.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			rs = null;
		}
	}
	
	private static void closeSt(Statement st){
		try {
			if(st != null){
				st.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			st = null;
		}
	}
	
	private static void closeConn(Connection conn){
		try {
			if(conn != null){
				conn.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally{
			conn = null;
		}
	}
}
           

C3P0Demo.java

package com.zhujunwei;

import java.sql.Connection;
import java.sql.PreparedStatement;
import org.junit.Test;
import com.zhujunwei.util.C3P0Util;


public class C3P0Demo {
	@Test
	public void testC3P0() {
		Connection conn = null ;
		PreparedStatement ps = null ;
		try {
			
			//得到连接对象
			conn = C3P0Util.getConnection();
			
			String sql = "insert into account values(null,?,?)";
			ps = conn.prepareStatement(sql);
			ps.setString(1, "c3p0");
			ps.setInt(2, 100);
			ps.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			C3P0Util.release(conn, ps);
		}
	}
}
           

结果

一切正常!

C3P0连接池1.导入所需的jar文件2.项目目录结构

数据库

一切ok!

C3P0连接池1.导入所需的jar文件2.项目目录结构