数据库连接池
C3P0
DBCP
DBUtil
有关数据库连接池:
1.为什么要使用数据库连接池?
资源重用性、自己创建消耗资源
便于管理数据库的连接
2.两种实现方式:C3P0数据库连接池 和 DBCP数据库连接池
如上的两种方式都是属于第三方的jar包,故而在使用之前都需要导包,当然连接第三方数据库也需要导入第三方数据库的相关驱动。
C3P0:
一般解压C3P0的压缩包,在其lib包下的c3p0-0.9.1.2.jar,就是需要的jar包。
ComboPoolDataSource 类是第三方提供的DataSource接口的实现类。
简单的获取数据库连接方式:
ComboPooledDataSource cpds = new ComboPooledDataSource();
cpds.setDriverClass("com.mysql.jdbc.Driver");
cpds.setJdbcUrl("jdbc:mysql://localhost:3306/test");
cpds.setUser("root");
cpds.setPassword("wang");
Connection conn = cpds.getConnection();
另外还可以使用配置文件的方式获取链接:
DataSource cpds = new ComboPooledDataSource("helloc3p0");
Connection conn = cpds.getConnection();
其中配置文件c3p0-config.xml位于src目录下(非project目录下)
<?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
<named-config name="helloc3p0">
<!-- 获取连接的4个基本信息 -->
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/test</property>
<property name="user">root</property>
<property name="password">abc123</property>
<!-- 当连接池中的连接数不够时,c3p0数据源一次性向数据库服务器申请的连接数 -->
<property name="acquireIncrement">5</property>
<!-- 初始化时,数据库连接池中的连接数 -->
<property name="initialPoolSize">10</property>
<!-- 连接池中维护的最少的连接数 -->
<property name="minPoolSize">5</property>
<!-- 连接池中维护的最大的连接数 -->
<property name="maxPoolSize">100</property>
<!-- 连接池中最多可以维护的Statement的个数 -->
<property name="maxStatements">100</property>
<!-- 每一个连接最多可以维护的Statement的个数 -->
<property name="maxStatementsPerConnection">2</property>
</named-config>
</c3p0-config>
DBCP 数据库连接池:
使用DBCP数据库连接池,需要导入两个jar包,
commons-dbcp-1.4下的commons-dbcp-1.4.jar 和
commons-pool-1.5.5包下的 commons-pool-1.5.5.jar
BasicDataSource类也同样是第三方提供的实现了DataSource接口的类。
简单获取数据库连接的方式:
BasicDataSource source = new BasicDataSource();
source.setDriverClassName("com.mysql.jdbc.Driver");
source.setUrl("jdbc:mysql://localhost:3306/test");
source.setUsername("root");
source.setPassword("abc123");
source.setMaxActive(50);
source.setInitialSize(10);
Connection conn = source.getConnection();
使用配置文件获取数据库连接的方式:
Properties pros = new Properties();
pros.load(this.getClass().getClassLoader()
.getResourceAsStream("dbcp.properties"));
DataSource source = BasicDataSourceFactory.createDataSource(pros);
Connection conn = source.getConnection();
DBUtils
第三方的操作数据库的jar包,其中封装了对数据库的操作。
常用类QueryRunner类。
public int update(Connection con,String sql,Object.. args)返回此操作影响的记录数目。
public < T> T query(Connection con,String sql,ResultSetHandler rsh ,Object …args) 其返回sql语句的查询结果,结果保存在实现了ResultSetHandler接口的具体类中。该对象用来将结果集转换成一个java对象。
如果只返回一个记录,可以将一条记录封装到一个对应类的对象里面,那么就可以使用BeanHandler对象来接受。
如果返回多条记录,可以使用BeanListHandler对象来接受。
如果返回一条记录,那么这条记录与表头上下同样构成了一个Map,可以使用MapHandler来接受。
如果返回多条记录,可以使用MapListHandler来接受。
如果返回记录中的特殊值,可以使用ScalarHandler来接受。
public ScalarHandler(String columnName);
public ScalarHandler();
public BeanHandler(Class clazz);
返回一条记录
Connection conn = DBCP.getConnection();
QueryRunner qr = new QueryRunner();
String sql = “select id,name,email,birth from customers where id = ?”;
ResultSetHandler< Customer> rsh= new BeanHandler< Customer>(Customer.class);
Customer cus = qr.query(conn, sql, rsh, 1);
返回多条结果
Connection conn = DBCP.getConnection();
QueryRunner qr = new QueryRunner();
String sql = "select id ,name,email ,birth from customers where id < ?";
ResultSetHandler< List< Customer>> rsh = new BeanListHandler< Customer>(Customer.class);
List< Customer> list = qr.query(conn, sql, rsh, 5);
使用ScalarHandler来接受返回特殊结果(首行首列的数值)
Connection conn = DBCP.getConnection();
QueryRunner qr = new QueryRunner();
String sql = “select count(*) from customers”;
ResultSetHandler rsh = new ScalarHandler();
Object resul = qr.query(conn, sql, rsh);