数据库连接池,DBUtils,PreparedStatement介绍

一.数据库连接池介绍
二.DBUtil介绍
三.示例代码
四.PreparedStatement补充

一.数据库连接池介绍
       使用jdbc进行数据库操作的时候,每次都要获取连接对象,操作完毕后都要关闭连接对象,这是一种耗费资源的操作。可以再系统启动的时候创建一批连接对象 都保存在一个集合里面,如果需要到数据库连接,就可以从这些对象中获取一个使用,数据库操作完毕,再将这些对象放回集合中去,这样就可以节省资源。对于使 用连接池,规模越大,效率越高。

       连接池的产品有dbcp,C3P0,poolman等,但是dbcp多用于教学,poolman比较少用,C3P0最为常用

使用方法:

1. 将c3p0相关文件添加到编译路径。

2. 在src文件夹下创建并配置c3p0.cfg.xml文件。

3. 自己创建一个类入(比如我创建的DBUtil类),创建唯一的DataSource接口对象。

4. 数据库操作时,先使用DataSource对象获得连接Connection连接对象。数据库操作完毕后调用Connection对象的close方法释放Connection对象。


二.DBUtil介绍

       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);
	}



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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值