您好,欢迎来到软文情感网。
搜索
您的当前位置:首页c3p0开源数据库连接池(DataSource)

c3p0开源数据库连接池(DataSource)

来源:软文情感网


②C3P0 数据库连接池(使用最简单方便) ③Apache Tomcat内置的连接池(apache dbcp)

实际应用时不需要编写连接数据库代码,直接从数据源获得数据库的连接。程序员编程时也应尽量使用这些数据源的实现,以提升程序的数据库访问性能。

使用时,需要新建java工程,在工程中建立“lib”目录,其中添加c3p0-0.9.1.2.jar和mysql-connector-java-5.0.8-bin.jar(mysql驱动)包,并add to build path。

第一种方法:不使用xml配置文件。

/**
 * 演示c3p0的使用方法
 * @project_name Day11 
 * @class_name C3P0Demo 
 * @author Dovinya
 * @data 2014-8-27 下午07:57:42 
 * @version 1
 * @notes
 */
public class C3P0Demo {
	
	@Test
	public void operateDatabase() {
	Connection conn =null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	try {
//	Class.forName("com.mysql.jdbc.Driver");
//	conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/day11", "root", "123");
//	ps = conn.prepareStatement("select * from account");
	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	
	dataSource.setDriverClass("com.mysql.jdbc.Driver");
	dataSource.setJdbcUrl("jdbc:mysql://localhost:3306/day11");
	dataSource.setUser("root");
	dataSource.setPassword("123");
	
	
	conn = dataSource.getConnection();
	ps = conn.prepareStatement("select * from account");
	rs = ps.executeQuery();
	
	while(rs.next()){
	String name = rs.getString("name");
	System.out.println(name);
	}
	
	} catch (Exception e) {
	e.printStackTrace();
	}finally{
	if(rs!=null){
	try {
	rs.close();
	} catch (SQLException e) {
	
	e.printStackTrace();
	}finally{
	rs=null;
	}
	}
	
	if(ps!=null){
	try {
	ps.close();
	} catch (SQLException e) {
	
	e.printStackTrace();
	}finally{
	ps=null;
	}
	}
	
	if(conn!=null){
	try {
	conn.close();
	} catch (SQLException e) {
	
	e.printStackTrace();
	}finally{
	conn=null;
	}
	}	
	
	}
	}
	
}
第二种方法:使用xml配置文件,这种方法更常见和普遍。

先新建xml文件,命名为c3p0-config.xml,在其中添加如下代码:



	
	com.mysql.jdbc.Driver
	jdbc:mysql:///day11
	root
	123

	3 
	10 
	2 
	10 
	
然后,新建java文件,在其中添加如下代码:
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
 * 演示c3p0的使用方法
 * @project_name Day11 
 * @class_name C3P0Demo 
 * @author Dovinya
 * @data 2014-8-27 下午07:57:42 
 * @version 1
 * @notes
 */
public class C3P0Demo {
	
	@Test
	public void operateDatabase() {
	Connection conn =null;
	PreparedStatement ps = null;
	ResultSet rs = null;
	try {

	ComboPooledDataSource dataSource = new ComboPooledDataSource();
	
	conn = dataSource.getConnection();
	ps = conn.prepareStatement("select * from account");
	rs = ps.executeQuery();
	
	while(rs.next()){
	String name = rs.getString("name");
	System.out.println(name);
	}
	
	} catch (Exception e) {
	e.printStackTrace();
	}finally{
	if(rs!=null){
	try {
	rs.close();
	} catch (SQLException e) {
	
	e.printStackTrace();
	}finally{
	rs=null;
	}
	}
	
	if(ps!=null){
	try {
	ps.close();
	} catch (SQLException e) {
	
	e.printStackTrace();
	}finally{
	ps=null;
	}
	}
	
	if(conn!=null){
	try {
	conn.close();
	} catch (SQLException e) {
	
	e.printStackTrace();
	}finally{
	conn=null;
	}
	}	
	
	}
	}
	
}
开发时常用。

Copyright © 2019- ruangwengfa.com 版权所有

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务