(三)JDBC连接池&DBUtils—C3P0的使用

(三)JDBC连接池&DBUtils—C3P0的使用

C3P0开源免费的连接池!目前使用他的开源项目有:SpringHibernate等。使用第三方工具需要导入jar包,C3P0使用时还需要添加配置文件c3p0-config.xml

通过这个配置来加载驱动和获取连接

C3P0配置文件

需要从官网下载平常可以自己收藏一份来使用

<?xml version="1.0" encoding="UTF-8"?>

<c3p0-config>

 

<default-config>

<property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="jdbcUrl">jdbc:mysql:///web08</property>

<property name="user">root</property>

<property name="password">123456</property>

<property name="initialPoolSize">5</property>

<property name="maxPoolSize">20</property>

</default-config>

 

<named-config name="itheima">

<property name="driverClass">com.mysql.jdbc.Driver</property>

<property name="jdbcUrl">jdbc:mysql:///web08</property>

<property name="user">root</property>

<property name="password">123456</property>

</named-config>

</c3p0-config>

这是比较常用的配置

具体参数有:

 

常见配置项:

 

 


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) {

// TODO Auto-generated catch block

throw new RuntimeException(e);

}

}

}


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) {
			// TODO Auto-generated catch block
			throw new RuntimeException(e);
		}
	}
	
}
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, "德玛");
			pstmt.setString(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);
		}
	}
	
	
	@Test
	public void testAddUser() {
		Connection conn = null;
		PreparedStatement pstmt = null;
		// 1.创建C3P0连接池对象
		//在ComboPooledDataSource()方法中参数部分是C3P0配置文件里面的 名字
		//加载默认的配置
		ComboPooledDataSource dataSource=new ComboPooledDataSource();
		
		//加载有名称的配置
		//ComboPooledDataSource dataSource1=new ComboPooledDataSource("itheima");

		try {
			// 2.从池子中获取连接
			conn = dataSource.getConnection();
			String sql = "insert into tbl_user values(null,?,?)";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, "赵信");
			pstmt.setString(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);
		}
	}
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值