连接池的产品有dbcp,C3P0,poolman等,但是dbcp多用于教学,poolman比较少用,C3P0最为常用。
使用方法:
1. 将c3p0相关文件添加到编译路径。
2. 在src文件夹下创建并配置c3p0.cfg.xml文件。
3. 自己创建一个类入(比如我创建的DBUtil类),创建唯一的DataSource接口对象。
4. 数据库操作时,先使用DataSource对象获得连接Connection连接对象。数据库操作完毕后调用Connection对象的close方法释放Connection对象。
DBUtils是java编程中的数据库操作使用工具,小巧简单实用。对于数据库表的操作,可以把结果转换成List,Array,Set等Java集合。
其中有几个重要的类,简要介绍,具体可以百度
1. QueryRunner---用来做查询,更新操作。
2. BeanHandle---将ResultSet中的一行数据转换成类对象。(平常的登录操作可以用这个,可以返回一个类对象)
3. BeanListHandle---将ResultSet中的所有数据转换成list,list存放的是类对象(比如说返回一张表的全部内容,就可以用这个,比较方便)
4. ArrayHandle---将ResultSet的第一行数据转换成对象数组。
5. ArrayListHandle---将ResultSet的所有数据转换成list,list存放的是Object[]
1. 将c3p0相关文件添加到编译路径,资源链接:http://download.csdn.net/detail/u014077165/7665097
2. 在src文件夹下创建并配置c3p0.cfg.xml文件,其中
基本配置:
<propertyname="driverClass">com.mysql.jdbc.Driver </property> ----你的数据库驱动
<property name="jdbcUrl">jdbc:mysql://localhost:3306/drink</property> ---你的数据库
<propertyname="user">root</property> ----数据库账户
<propertyname="password">root</property> ----数据库密码
<propertyname="checkoutTimeout">30000</property> ---配置当连接池所有连接用完时应用程序getConnection的等待时间。为0时则无限等待,直到有其他连接释放,不为0的时候,如果仍然没有获 得连接,则抛出SQLException。
<propertyname="idleConnectionTestPeriod">30</property> ---用来配置测试空闲连接的时间,可以用来解决MySql8小时断开连接的问题。因为它保证连接池会每隔一段时间对空闲连接进行一次测试,从而保证有效 的空闲连接能每隔一段时间访问一次数据库,将于MySql8小时无会话状态打破。值为0则不测试。
<property name="initialPoolSize">10</property> ---连接池初始化时创建的连接数
<propertyname="maxIdleTime">30</property> ---连接的最大空闲时间,如果超过这个时间,某个数据库连接还没有被使用,则会断开掉这个连接。如果为0,则不会断开连接。
<property name="maxPoolSize">100</property> ---连接池中拥有的最大连接数,如果获得新连接时会时连接总数超过这个值则不会再获取连接,而是等待
<property name="minPoolSize">10</property> ---连接池保持的最小连接数。
<propertyname="maxStatements">200</property& gt; ---连接池为数据源缓存的PreparedStatement的总数。由于PreparedStatement属于单个Connection,所以这个 数量应该根据应用中平均连接数乘以每个连接的平均PreparedStatement来计算。
如图:
3. 自己创建类,创建唯一的DataSouce对象(也就是连接池对象)。如下代码,使用static final修饰DataSource,并使用静态初始化块进行初始化。初始化完成后,便可以调用DBUtils这个工具,使用QueryRunner的 ds得到连接池对象,再进行增删查改功能。
package com.niit.drinkshop.dao.impl;
import java.sql.SQLException;
import javax.sql.DataSource;
import org.apache.commons.dbutils.QueryRunner;
import com.mchange.v2.c3p0.ComboPooledDataSource;
/**
* 给系统提供一个唯一的数据源(连接池对象)
*
*/
public class DBUtil {
private static final DataSource ds;
static{
ds = new ComboPooledDataSource();
}
public static DataSource getDataSource(){
return ds;
}
public static int update(String sql,Object... params) throws SQLException{
return new QueryRunner(ds).update(sql, params);
4. 具体使用:如下代码,便可以直接调用update方法更新,十分方便。
/**
* 更新一个饮料
* @throws SQLException
*/
@Override
public int update(Drink t) throws SQLException {
String sql = "update drink set size = ? where id = ?";
return DBUtil.update(sql,t.getSize(),t.getId());
}
</pre><p><strong></strong></p><div style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><strong style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: 微软雅黑; font-style: normal; text-align: left;"><span style="font-size:18px;background-color: inherit;">四.<strong style="background-color: inherit;">PreparedStatement补充</strong></span></strong></div><div style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><p style="margin: 5px 0px; background-color: inherit; text-align: justify;"><span style="background-color: inherit; font-size: 10.5pt;"><span style="font-family:Microsoft Yahei;background-color: inherit;"> <span style="color:#ff0000;background-color: inherit;"><strong style="background-color: inherit;">为什么要使用PreparedStatement</strong></span>,<span style="color:#4f81bd;background-color: inherit;"><strong style="background-color: inherit;">因为我们每向数据库发送一条sql语句,那么数据库的解释器就会把这个sql语句翻译成自己的底层语言,然后数据库再执行这个底层语言,无形中就是增加了数据库的压力,降低了访问速度。</strong></span></span></span></p><p style="margin: 5px 0px; background-color: inherit; text-align: justify;"><span style="background-color: inherit; font-size: 10.5pt;"><span style="font-family:Microsoft Yahei;background-color: inherit;"> 那么我们就考虑能否在应用程序中就把sql语句先进行翻译,然后数据库就可以直接执行。那么PreparedStatement就是解决这个问题。</span></span></p><p style="margin: 5px 0px; background-color: inherit; text-align: justify;"><span style="background-color: inherit; font-size: 10.5pt;"><span style="font-family:Microsoft Yahei;background-color: inherit;"> 首先使用PreparedStatement声明一个对象pst,然后调用Connection实例的preparedStatement(String sql)方法获得一个PreparedStatement对象,此时要把sql语句作为一个参数传给方法。最后pst就可以正常调用executeQuery();等方法返回ResultSet结果集。</span></span></p><br style="background-color: inherit;" /></div><div style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><strong style="background-color: inherit;">总结:</strong></div><div style="background-color: rgb(255, 255, 255); color: rgb(0, 0, 0); font-family: 微软雅黑; font-size: 14px; font-style: normal; font-variant: normal; font-weight: normal; letter-spacing: normal; line-height: 21px; orphans: auto; text-align: left; text-indent: 0px; text-transform: none; white-space: normal; widows: auto; word-spacing: 0px; -webkit-text-stroke-width: 0px;"><p style="margin: 5px 0px; background-color: inherit;">1. 首先我们要创建一个连接池,这样我们就不用总是创建连接,销毁连接了。创建方法就是<span style="color:red;background-color: inherit;">创建一个</span><span style="color:red;background-color: inherit;">c3p0</span><span style="color:red;background-color: inherit;">文件</span>,做好前提工作。</p><p style="margin: 5px 0px; background-color: inherit;">2. 就是要<span style="color:red;background-color: inherit;">创建一个连接池对象</span>。比如上面代码中的</p></div><p align="left"></p><pre name="code" class="java">static{
ds = new ComboPooledDataSource();
}
3. 用DBUtils得到连接池对象。为什么要用DBUtils得到,就是因为我们要用DBUtils来方便我们编程,对数据库进行增删查改。比如代码:
new QueryRunner(ds)
4. 使用DBUtils对数据库进行增删查改。
public int update(Drink t) throws SQLException {
String sql = "update drink set size = ? where id = ?";
return DBUtil.update(sql,t.getSize(),t.getId());
}
public static int update(String sql,Object... params) throws SQLException{
return new QueryRunner(ds).update(sql, params);
}