06_C3P0连接池

 
package cn.itheima.jdbc.utils;
/**
 * 提供获取连接和释放资源的方法
 * @author XING
 *
 */

import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Properties;
import java.util.ResourceBundle;

public class JDBCUtils_V3 {
	private static String driver;
	private static String url;
	private static String username;
	private static String password;

	/**
	 * 静态代码块加载配置文件信息
	 */
	static {
		try {
			// 1.通过当前类获取加载器
			ClassLoader classLoader = JDBCUtils_V3.class.getClassLoader();
			// 2.通过类加载器的方法获得一个输入流
			InputStream is = classLoader.getResourceAsStream("db.properties");
			// 3.创建一个properties对象
			Properties props = new Properties();
			// 4.加载输入流
			props.load(is);
			// 5.获取相关参数的值
			driver = props.getProperty("driver");
			url = props.getProperty("url");
			username = props.getProperty("username");
			password = props.getProperty("password");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	/**
	 * 获取连接方法
	 * 
	 * @return
	 */
	public static Connection getConnection() {
		Connection conn = null;
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url, username, password);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return conn;
	}

	public static void release(Connection conn, PreparedStatement pstmt, ResultSet rs) {
		if (rs != null) {
			try {
				rs.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (pstmt != null) {
			try {
				pstmt.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}
 



  
    com.mysql.jdbc.Driver
	jdbc:mysql:///web08
	root
	root
	5
	20
  
  
   
    com.mysql.jdbc.Driver
	jdbc:mysql:///web08
	root
	root
  
  

 

 

 

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 testAddSource1(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		try {
			//2.从工具类中获取
			conn = C3P0Utils.getConnection();
			//3.编写SQL语句
			String sql = "insert into tbl_user values(null,?,?)";
			//4.获取执行SQL语句对象
			pstmt = conn.prepareStatement(sql);
			//5.设置参数
			pstmt.setString(1, "吕布3");
			pstmt.setString(2, "貂蝉3");
			//6.执行添加操作
			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);
		}
	}
	
	@Test
	public void testAddSource(){
		Connection conn = null;
		PreparedStatement pstmt = null;
		//1.创建c3p0连接池对象
		ComboPooledDataSource dataSource = new ComboPooledDataSource();//加载默认的配置
		//ComboPooledDataSource dataSource = new
		//ComboPooledDataSource dataSource = new ComboPooledDataSource("itheima");//加载有名称的配置
		try {
			//2.从池子中获取连接
			conn = dataSource.getConnection();
			//3.编写SQL语句
			String sql = "insert into tbl_user values(null,?,?)";
			//4.获取执行SQL语句对象
			pstmt = conn.prepareStatement(sql);
			//5.设置参数
			pstmt.setString(1, "吕布2");
			pstmt.setString(2, "貂蝉2");
			//6.执行添加操作
			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);
		}
	}
}
 
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);
		}
	}
}

 

 

 

 

 

你可能感兴趣的:(JAVA笔记)