DbUtil

之前就听别人说过Dbutil这个Apache开源组件很好用.一直也没时间来自己实践下,今正好周末加上圣诞就在家好好来研究了一下.最新的版本是1.3,其下在地址是:[url]http://commons.apache.org/dbutils/download_dbutils.cgi[/url]
其jar结构
[img]http://dl.iteye.com/upload/attachment/376122/97bfbaf0-9905-3293-ad45-9291b2549ca9.png[/img]
commons.dbutils是一个对JDBC操作进行封装的类集,不要懒其它的jar,避免了重复繁琐的JDBC代码.

这里基本用到的类是QueryRunner,ResultSetHandler及其子类

[color=red]1.ArrayHandler:把结果集中的第一行数据转成对象数组。

2.ArrayListHandler:把结果集中的每一行数据都转成一个对象数组,再存放到List中。

3.BeanHandler:将结果集中的第一行数据封装到一个对应的JavaBean实例中。

4.BeanListHandler:将结果集中的每一行数据都封装到一个对应的 JavaBean实例中,存放到List里。

5.ColumnListHandler:将结果集中某一列的数据存放到List中。

6.KeyedHandler:将结果集中的每一行数据都封装到一个Map里,然后再根据指定的key把每个Map再存放到一个Map里。

7.MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值。

8.MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List。

9.ScalarHandler:将结果集中某一条记录的其中某一列的数据存成 Object。[/color]

这是我的demo结构图:
[img]http://dl.iteye.com/upload/attachment/376132/cb7e1ca3-e9ea-3956-897b-cdfb3fd1d929.png[/img]
============= 数据库帮助类,是不是比JDBC更写的代码更少更简洁==========
package com.huawei.dbUtils;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DbUtileutil {
static{
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection()
{
String url="jdbc:mysql://127.0.0.1:3306/db_votemanage?useUnicode=true&characterEncoding=UTF-8";
Connection conn = null;
try {
conn=DriverManager.getConnection(url, "root", "root");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return conn;

}
}
[color=red]DbUtil的CRUD方法:[/color]
package com.huawei.daoImpl;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import sun.security.x509.ReasonFlags;
import com.huawei.bean.Channel;
import com.huawei.dbUtils.DbUtileutil;
/**
* @name 何枫
* @date 2010-12-17
* @action VoteDAOImpl.java
* @time 下午16:18:52
* @package_name com.huawei.DbUtil
* @project_name DbUtilTest
*/
public class VoteDAOImpl {

// 以下部分代码采用ArrayHandler存储方式查询
public void testArrayHandler() {
System.out.println("------testArrayHandler----");
String sql = "select *from tb_channel";
ResultSetHandler handler = new ArrayHandler(); // 返回一行的object[]
// ResultSetHandler handler = new
// ArrayListHandler();//返回list,每一行的object[]
QueryRunner query = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn = null;
conn = util.getConnection();
try {
Object[] arr = (Object[]) query.query(conn, sql, handler);
for (int i = 0; i < arr.length; i++) {
System.out.println(arr[i].toString());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// ignore
}
}

}
// 以下部分代码采用ArrayListHandler存储方式查询
public void testArrayListHandler() {
System.out.println("------testArrayHandler----");
String sql = "select *from tb_channel";
ResultSetHandler handler = new ArrayListHandler();// 返回list,每一行的object[]
QueryRunner query = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn = null;
conn = util.getConnection();
try {
List array = (List) query.query(conn, sql, handler);
for (int j = 0; j < array.size(); j++) {
Object[] arr = (Object[])array.get(j);
/*for (int k = 0; k < arr.length; k++) {
System.out.println(arr[k].toString());
}*/
System.out.println(arr[0].toString()+"========="+arr[1].toString());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*====================================================*/
// 以下部分代码采用BeanHandler存储方式查询
public void testBeanHandler() {
System.out.println("-------testBeanHandler------");
String sqlBeanListHandler = "select *from tb_channel where channelID=?";
Object[] arr = new Object[] { "7" };
ResultSetHandler handler = new BeanListHandler(Channel.class);
QueryRunner runner = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn = null;
conn = util.getConnection();
try {
List list = (List) runner.query(conn, sqlBeanListHandler, arr,handler);
for (int i = 0; i < list.size(); i++) {
Channel user = (Channel) list.get(i);
System.out.println(user.getChannelID() + " "+ user.getChannelName());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*====================================================*/
// 以下部分代码采用BeanListHandler存储方式查询
public void testBeanListHandler() {
System.out.println("------BeanListHandler-----");
String sql = "select *from tb_channel";
ResultSetHandler handler = new BeanListHandler(Channel.class);
QueryRunner query = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn = null;
conn = util.getConnection();
try {
List list = (List) query.query(conn, sql, handler);
for (int i = 0; i < list.size(); i++) {
Channel channel = (Channel) list.get(i);
System.out.println(channel.getChannelID() + "============"+ channel.getChannelName());
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
/*====================================================*/
// 以下部分代码采用KeyeHandler存储方式查询
public void testKeyeHandlet()
{
System.out.println("--------testKeyeHandlet--------");
String sql = "select *from tb_channel";
ResultSetHandler handler = new KeyedHandler("channelID");
QueryRunner runner = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn=null;
conn=util.getConnection();
try {
Map map = (Map)runner.query(conn, sql, handler);
Map user = (Map)map.get(3);
System.out.println(user.get("channelName"));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*====================================================*/
// 以下部分代码插入一条数据
public void insert()
{
System.out.println("-------Insert---------");
String sql ="insert into tb_channel(channelName)values('亚洲杯')";
QueryRunner query = new QueryRunner();
DbUtileutil utileutil = new DbUtileutil();
Channel channel = new Channel();
Connection conn=null;
conn=utileutil.getConnection();
//Object[] param = new Object[]{channel.getChannelID(),channel.getChannelName()};
try {
query.update(conn, sql);
//query.update(conn, sql, param);
System.out.println("======Insert添加成功!!!======");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*====================================================*/
// 以下部分代码更新一条数据
public void upload()
{
System.out.println("---------upload更新数据库----------");
String sql ="update tb_channel set channelName='德甲'where channelID=9";
QueryRunner query = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn=null;
conn=util.getConnection();
try {
query.update(conn, sql);
System.out.println("==========更新数据库成功============");
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
finally
{
try {
DbUtils.close(conn);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
/*====================================================*/
// 以下部分代码删除一条数据
public void delete()
{
System.out.println("------delete----------");
String sql ="delete from tb_channel where channelID=1";
QueryRunner query = new QueryRunner();
DbUtileutil util = new DbUtileutil();
Connection conn=null;
conn=util.getConnection();
try
{
query.update(conn, sql);
System.out.println("========数据删除成功!!!=========");
} catch (SQLException ex) {
ex.printStackTrace();
try {
DbUtils.rollback(conn);
} catch (SQLException e) {
e.printStackTrace();
}
} finally {
DbUtils.closeQuietly(conn);
}

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值