C3P0连接池
-
C3P0是一个开源的JDBC连接池,它实现了数据源和JNDI绑定,支持JDBC3规范和JDBC2的标准扩展。目前使用它的有Hibernate,Spring等。
-
在src下建立c3p0-config.xml文件,进行如下配置
<c3p0-config>
<default-config>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/excel</property>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="user">root</property>
<property name="password">root</property>
<property name="initialPoolSize">5</property>
<property name="maxPoolSize">10</property>
<property name="maxIdleTime">100</property>
</default-config>
</c3p0-config>
- 使用模板如下,有两种创建方式
package cn.hsx.edu.c3p0;
import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.junit.Test;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* C3p0连接池
* @author ssk
*
*/
public class C3p0 {
/**
* 使用硬编码方式,实现C3P0连接
*/
@Test
public void test() throws PropertyVetoException, SQLException {
//创建连接池核心类
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//设置连接参数
dataSource.setJdbcUrl("jdbc:mysql//localhost:8080/excel");
dataSource.setDriverClass("com.mysql.jdbc.Driver");
//同户名
dataSource.setUser("root");
//密码
dataSource.setPassword("root");
//初始化连接数量
dataSource.setInitialPoolSize(5);
//最大连接数量
dataSource.setMaxPoolSize(10);
//最大空闲时间
dataSource.setMaxIdleTime(1000);
//获得连接
Connection con = dataSource.getConnection();
con.prepareStatement("delete from score where id = 1").executeQuery();
con.close();
}
/**
* 使用c3p0-config.xml配置连接,必须放在src目录下
*/
@Test
public void testXML() throws SQLException {
// 创建c3p0连接池核心工具类
// 自动加载src下c3p0的配置文件【c3p0-config.xml】
ComboPooledDataSource dataSource = new ComboPooledDataSource();
//获得连接
Connection con = dataSource.getConnection();
String sql = "select * from score where id = ?";
PreparedStatement stmt = null;
for(int i=0;i<15;i++) {
stmt = con.prepareStatement(sql);
stmt.setInt(1, i);
ResultSet rs = stmt.executeQuery();
while(rs.next()) {
System.out.println(rs.getString("name")+","+rs.getString("address"));
}
}
}
}
DBCP连接池
-
DBCP(DataBase Connection Pool)数据库连接池,是java数据库连接池的一种,由Apache开发,通过数据库连接池,可以让程序自动管理数据库连接的释放和断开。
-
在当前目录下新建db.properties文件,如下
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost/excel
username=root
password=root
initialSize=5
maxActive=10
maxIdle=3000
- 使用模板如下,有两种实现方式
package cn.hsx.edu.dbcp;
import java.io.InputStream;
import java.sql.Connection;
import java.util.Properties;
import javax.sql.DataSource;
import org.apache.commons.dbcp.BasicDataSource;
import org.apache.commons.dbcp.BasicDataSourceFactory;
import org.junit.Test;
/**
* DBCP连接池
* @author ssk
*/
public class DBCP {
/**
* 硬编码实现连接
*/
@Test
public void testDbcp1() throws Exception {
//BasicDataSource是DBCP的核心类
BasicDataSource dataSource = new BasicDataSource();
dataSource.setDriverClassName("com.mysql.jdbc.Driver");
dataSource.setUrl("jdbc:mysql://localhost/excel");
dataSource.setUsername("root");
dataSource.setPassword("root");
dataSource.setInitialSize(5);// 初始化连接
dataSource.setMaxActive(10);// 最大连接
dataSource.setMaxIdle(3000);// 最大空闲时间
// 获取连接
Connection con = dataSource.getConnection();
con.prepareStatement("delete from score where id = 7").executeUpdate();
con.close();
System.out.println("删除完成");
}
/**
* 通过Properties配置文件实现连接
*/
@Test
public void testDbcp2() throws Exception {
// 加载prop配置文件
Properties prop = new Properties();
// 获取文件流
InputStream inStream = DBCP.class.getResourceAsStream("db.properties");
// 加载属性配置文件
prop.load(inStream);
// 根据prop配置,直接创建数据源对象
DataSource dataSource = BasicDataSourceFactory.createDataSource(prop);
Connection con = dataSource.getConnection();
con.prepareStatement("delete from score where id = 6").executeUpdate();
con.close();
System.out.println("删除完成");
}
}
网友评论