Jakarta Commons组件BeanUtils、DbUtils简化JDBC数据库操作

本文介绍如何使用Jakarta Commons beanutils和dbutils简化JDBC数据库操作。通过实例展示了beanutils的ResultSetDynaClass与RowSetDynaClass及dbutils的QueryRunner在数据库查询中的应用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

虽然现在出现了很多ORM框架,可是还是有很多朋友也许还在使用JDBC,就像我现在一样,除了学习的时候在使用Hibernate、Spring类似这些优秀的框架,工作时一直都在使用JDBC。本文就简单介绍一下利用Jakarta Commons旗下beanutils、dbutils简化JDBC数据库操作,以抛砖引玉,希望对像我一样在使用JDBC的朋友有所帮助。

下面就分两部分简单介绍beanutils、dbutils在基于JDBC API数据库存取操作中的运用。第一部分显介绍beanutils在JDBC数据库存取操作中的运用,第二部分介绍dbutils在JDBC数据库存取操作中的运用,最后看看他们的优缺点,谈谈本人在项目运用过程中对他们的一点心得体会,仅供参考,其中有错误的地方希望大虾不吝赐教,大家多多交流共同进步。

一、Jakarta Commons beanutils

Beanutils是操作Bean的锐利武器,其提过的BeanUtils工具类可以简单方便的读取或设置Bean的属性,利用Dyna系列,还可以在运行期创建Bean,符合懒人的习惯,正如LazyDynaBean,LazyDynaClass一样,呵呵。这些用法已经有很多文章提及,也可以参考apache的官方文档。

对于直接利用JDBC API访问数据库时(这里针对的是返回结果集ResultSet的查询select),大多数都是采用两种方式,一种是取出返回的结果集的数据存于Map中,另一种方式是Bean里。针对第二种方式,Beanutils里提供了ResultSetDynaClass结合DynaBean以及RowSetDynaClass结合DynaBean来简化操作。下面用以个简单的例子展示一下beanutils的这两个类在JDBC数据库操作中的运用。

请在本机建立数据库publish,我用的是MySQL,在publish数据库中建立表book,脚本如下:
CREATE TABLE book(
id int(11) NOT NULL auto_increment,
title varchar(50) character set latin1 NOT NULL,
authors varchar(50) character set latin1 default NULL,
PRIMARY KEY (id)
)

然后用你喜欢的编辑器建立一个类BeanutilsJDBCTest,我们先用ResultSetDynaClass来处理,然后再用RowSetDynaClass来实现同样的类,之后看看他们之间有什么不同,用ResultSetDynaClass处理的源代码如下所示:

package cn.qtone.test;

imp<wbr style="line-height:22px">ort java.sql.Connection;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.DriverManager;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.ResultSet;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.Statement;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.Iterator;<span> </span><br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.beanutils.DynaBean;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.beanutils.PropertyUtils;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.beanutils.ResultSetDynaClass;<span> </span><br style="line-height:22px"><br style="line-height:22px"> public class BeanutilsJDBCTest{<br style="line-height:22px"> public static void main(String[] args) {<br style="line-height:22px"> Connection con = null;<br style="line-height:22px"> Statement st = null;<br style="line-height:22px"> ResultSet rs = null;<br style="line-height:22px"> try {<br style="line-height:22px"> Class.forName("com.mysql.jdbc.Driver");<br style="line-height:22px"><br style="line-height:22px"> String url = "jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&amp;characterEncoding=GBK";<br style="line-height:22px"><br style="line-height:22px"> con = DriverManager.getConnection(url, "root", "hyys");<br style="line-height:22px"><br style="line-height:22px"> st = con.createStatement();<br style="line-height:22px"><br style="line-height:22px"> rs = st.executeQuery("select * from book");<br style="line-height:22px"><br style="line-height:22px"> ResultSetDynaClass rsDynaClass = new ResultSetDynaClass(rs);<br style="line-height:22px"><br style="line-height:22px"> Iterator itr = rsDynaClass.iterator();<br style="line-height:22px"><br style="line-height:22px"> System.out.println("title-------------authors");<br style="line-height:22px"><br style="line-height:22px"> while (itr.hasNext()) {<br style="line-height:22px"> DynaBean dBean = (DynaBean) itr.next();<br style="line-height:22px"> System.out.println(PropertyUtils.getSimpleProperty(dBean,"title")<br style="line-height:22px"> + "-------------"+ PropertyUtils.getSimpleProperty(dBean, "authors"));<br style="line-height:22px"><br style="line-height:22px"> }<br style="line-height:22px"> } catch (Exception e) {<br style="line-height:22px"> e.printStackTrace();<br style="line-height:22px"> } finally {<br style="line-height:22px"> try {<br style="line-height:22px"> if (rs != null) {<br style="line-height:22px"> rs.close();<br style="line-height:22px"> }<br style="line-height:22px"> if (st != null) {<br style="line-height:22px"> st.close();<br style="line-height:22px"> }<br style="line-height:22px"> if (con != null) {<br style="line-height:22px"> con.close();<br style="line-height:22px"> }<br style="line-height:22px"> } catch (Exception e) {<br style="line-height:22px"> e.printStackTrace();<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"><br style="line-height:22px"> }<span> </span><br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"> 用RowSetDynaClass处理的源代码如下所示:<span> </span><br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"> package cn.qtone.test;<span> </span><br style="line-height:22px"><br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.Connection;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.DriverManager;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.ResultSet;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.Statement;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.Iterator;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.List;<span> </span><br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.beanutils.DynaBean;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.beanutils.PropertyUtils;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.beanutils.RowSetDynaClass;<span> </span><br style="line-height:22px"><br style="line-height:22px"> public class BeanutilsJDBCTest{<br style="line-height:22px"> public static void main(String[] args) {<br style="line-height:22px"> List rsDynaClass = rsTest();<br style="line-height:22px"><br style="line-height:22px"> System.out.println("title ------------- authors ");<br style="line-height:22px"><br style="line-height:22px"> Iterator itr = rsDynaClass.iterator();<br style="line-height:22px"><br style="line-height:22px"> while (itr.hasNext()) {<br style="line-height:22px"><br style="line-height:22px"> DynaBean dBean = (DynaBean) itr.next();<br style="line-height:22px"><br style="line-height:22px"> try {<br style="line-height:22px"> System.out.println(PropertyUtils.getSimpleProperty(dBean,"name")<br style="line-height:22px"> + "-------------"+ PropertyUtils.getSimpleProperty(dBean, "mobile"));<br style="line-height:22px"><br style="line-height:22px"> } catch (Exception e) {<br style="line-height:22px"> // TODO 自动生成 catch 块<br style="line-height:22px"> e.printStackTrace();<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"> }<span> </span><br style="line-height:22px"><br style="line-height:22px"> private static List rsTest() {<br style="line-height:22px"><br style="line-height:22px"> Connection con = null;<br style="line-height:22px"> Statement st = null;<br style="line-height:22px"> ResultSet rs = null;<br style="line-height:22px"><br style="line-height:22px"> try {<br style="line-height:22px"> Class.forName("com.mysql.jdbc.Driver");<br style="line-height:22px"><br style="line-height:22px"> String url = "jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&amp;characterEncoding=GBK";<br style="line-height:22px"><br style="line-height:22px"> con = DriverManager.getConnection(url, "root", "hyys");<br style="line-height:22px"><br style="line-height:22px"> st = con.createStatement();<br style="line-height:22px"><br style="line-height:22px"> rs = st.executeQuery("select * from book");<br style="line-height:22px"><br style="line-height:22px"> RowSetDynaClass rsdc = new RowSetDynaClass(rs);<br style="line-height:22px"><br style="line-height:22px"> return rsdc.getRows();<br style="line-height:22px"><br style="line-height:22px"> } catch (Exception e) {<br style="line-height:22px"> e.printStackTrace();<br style="line-height:22px"> } finally {<br style="line-height:22px"> try {<br style="line-height:22px"> if (rs != null) {<br style="line-height:22px"> rs.close();<br style="line-height:22px"> }<br style="line-height:22px"> if (st != null) {<br style="line-height:22px"> st.close();<br style="line-height:22px"> }<br style="line-height:22px"> if (con != null) {<br style="line-height:22px"> con.close();<br style="line-height:22px"> }<br style="line-height:22px"> } catch (Exception e) {<br style="line-height:22px"> e.printStackTrace();<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"><br style="line-height:22px"> return null;<br style="line-height:22px"> }<br style="line-height:22px"> }<span> </span><br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"> 这两个方法输出的结果应该是一样的。但是很显然第二种方式比第一种方式要好,它把数据访问部分抽取出来放到一个方法中,显得简单清晰。<br style="line-height:22px"><br style="line-height:22px"> 其实在利用ResultSetDynaClass时,必须在ResultSet等数据库资源关闭之前,处理好那些数据,你不能在资源关闭之后使用DynaBean,否则就会抛出异常,异常就是说不能在ResultSet之后存取数据(具体的异常名我也忘了),当然你也可以采用以前的方式一个一个的把数据放到Map里,如果你一定要那样做,建议还是别用Beanutils,因为这没带给你什么好处。总之利用ResultSetDynaClass你的程序的扩展性非常部好。<br style="line-height:22px"><br style="line-height:22px"> 从第二中方式可以看出,利用RowSetDynaClass可以很好的解决上述ResultSetDynaClass遇到的问题,RowSetDynaClass的getRows()方法,把每一行封装在一个DynaBean对象里,然后,把说有的行放到一个List里,之后你就可以对返回的List里的每一个DynaBean进行处理,此外对于DynaBean你还可以采用标准的get/set方式处理,当然你也可以用PropertyUtils. getSimpleProperty(Object bean, String name)进行处理。<br style="line-height:22px"><br style="line-height:22px"> 从上面的分析中,你应该可以决定你应该使用ResultSetDynaClass还是RowSetDynaClass了。<br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"><br style="line-height:22px"> 未完待续……</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

=============================

二、Jakarta Commons dbutils:
用JDBC API时最令人讨厌的就是异常处理,也很烦琐,而且很容易出错,本人曾考虑过利用模板进行处理,后来看到了dbutils,之后就采用那个dbutils,采用模板的方式各位朋友可以参考Spring,Spring的JdbcTemplate不灵活而强大,呵呵,说句闲话,实在太佩服Rod Johnson了,Rod Johnson真的很令人尊敬。
Dbutils的QueryRunner把大多数与关闭资源相关的封装起来,另外,你也可以使用DbUtils进行关闭,当然DbUtils提供的功能当然不止这些,它提过了几个常用的静态方法,除了上述的关闭资源外,DbUtils. commitAndClose(Connection conn)还提供事务提及等操作。
还是以一个例子来说说吧,毕竟我不是搞业务的,小嘴巴吧嗒吧哒不起来啊,呵呵。
为了和采用Beanutils更好的进行对比,这个例子还是实现同样的功能,数据库同样采用前一篇文章中提到的publish。
同样的,用你喜欢的编辑器建立一个类DbutilsJDBCTest,示例代码如下所示:
package cn.qtone.test;
imp<wbr style="line-height:22px">ort java.sql.Connection;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.DriverManager;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.SQLException;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.List;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.Map;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.dbutils.DbUtils;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.dbutils.QueryRunner;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.dbutils.handlers.MapListHandler;<br style="line-height:22px"> public class DbutilsJDBCTest{<br style="line-height:22px"> public static void main(String[] args) {<br style="line-height:22px"> Connection conn = null;<br style="line-height:22px"> String jdbcURL = "jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&amp;characterEncoding=GBK";<br style="line-height:22px"> String jdbcDriver = "com.mysql.jdbc.Driver";<br style="line-height:22px"> try {<br style="line-height:22px"> DbUtils.loadDriver(jdbcDriver);<br style="line-height:22px"> // Username "root". Password "root"<br style="line-height:22px"> conn = DriverManager.getConnection(jdbcURL, "root", "root");<br style="line-height:22px"> QueryRunner qRunner = new QueryRunner();<br style="line-height:22px"> System.out.println("***Using MapListHandler***");<br style="line-height:22px"> //以下部分代码采用Map存储方式,可以采用Bean的方式代替进行处理<br style="line-height:22px"> List lMap = (List) qRunner.query(conn,<br style="line-height:22px"> "select title,authors from books", new MapListHandler());<br style="line-height:22px"> //以下是处理代码,可以抽取出来<br style="line-height:22px"> System.out.println("title ------------- authors ");<br style="line-height:22px"> for (int i = 0; i &lt; lMap.size(); i++) {<br style="line-height:22px"> Map vals = (Map) lMap.get(i);<br style="line-height:22px"> System.out.println(vals.get("title")+"-------------"+ vals.get("authors"));<br style="line-height:22px"> }<br style="line-height:22px"> } catch (SQLException ex) {<br style="line-height:22px"> ex.printStackTrace();<br style="line-height:22px"> } finally {<br style="line-height:22px"> DbUtils.closeQuietly(conn);<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"><br style="line-height:22px"> 怎么样?是不是比采用Beanutils的ResultSetDynaTrial和RowSetDynaClass好多了?采用Beanutils令人难缠的是关闭那些资源以及处理那些异常,而这里采用Dbutils显然代码量减少了很多。<br style="line-height:22px"> 上例在处理结果集时,它把数据库中的每一行映射成一个Map,其中列名作为Key,该列对应的值作为Value存放,查询的所有的数据一起放在一个List里,然后进行处理,当然,一个更明智的处理是直接返回List然后再单独进行处理。<br style="line-height:22px"> 事实上上例返回的结果集中的每一行不必放在一个Map里,你可以放在一个Bean里,当然如果你真的很懒,你也可以使用Beanutils的LazyDynaClass和LazyDynaBean,不过也许没有必要那么做,至于原因请看下文。<br style="line-height:22px"><br style="line-height:22px"> 如果使用Bean而不是用Map,那么,你也许需要建立一个Bean,如下:<br style="line-height:22px"> package cn.qtone.test;<br style="line-height:22px"> public class Book {<br style="line-height:22px"> public int id;<br style="line-height:22px"> public String title;<br style="line-height:22px"> public String authors ;<br style="line-height:22px"> public StudentBean() {<br style="line-height:22px"> }<br style="line-height:22px"> public String getAuthors() {<br style="line-height:22px"> return authors;<br style="line-height:22px"> }<br style="line-height:22px"> public void setAuthors(String authors) {<br style="line-height:22px"> this.authors = authors;<br style="line-height:22px"> }<br style="line-height:22px"> public int getId() {<br style="line-height:22px"> return id;<br style="line-height:22px"> }<br style="line-height:22px"> public void setId(int id) {<br style="line-height:22px"> this.id = id;<br style="line-height:22px"> }<br style="line-height:22px"> public String getTitle() {<br style="line-height:22px"> return title;<br style="line-height:22px"> }<br style="line-height:22px"> public void setTitle(String title) {<br style="line-height:22px"> this.title = title;<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"><br style="line-height:22px"> 然后简单修改一下DbutilsJDBCTest 中的部分代码即可,代替之后的源代码如下:<br style="line-height:22px"><br style="line-height:22px"> package cn.qtone.test;<br style="line-height:22px"><br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.Connection;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.DriverManager;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.sql.SQLException;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.List;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort java.util.Map;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.dbutils.DbUtils;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.dbutils.QueryRunner;<br style="line-height:22px"> imp<wbr style="line-height:22px">ort org.apache.commons.dbutils.handlers.BeanListHandler;<br style="line-height:22px"><br style="line-height:22px"> public class DbutilsJDBCTest{<br style="line-height:22px"> public static void main(String[] args) {<br style="line-height:22px"> Connection conn = null;<br style="line-height:22px"> String jdbcURL = "jdbc:mysql://127.0.0.1:3306/publish?useUnicode=true&amp;characterEncoding=GBK";<br style="line-height:22px"> String jdbcDriver = "com.mysql.jdbc.Driver";<br style="line-height:22px"> try {<br style="line-height:22px"> DbUtils.loadDriver(jdbcDriver);<br style="line-height:22px"> // Username "root". Password "root"<br style="line-height:22px"> conn = DriverManager.getConnection(jdbcURL, "root", "root");<br style="line-height:22px"> QueryRunner qRunner = new QueryRunner();<br style="line-height:22px"> System.out.println("***Using BeanListHandler ***");<br style="line-height:22px"> //以下部分代码采用Map存储方式,可以采用Bean的方式代替进行处理<br style="line-height:22px"> List lBeans = (List) qRunner.query(conn," select title,authors from books ", new BeanListHandler(Book.class));<br style="line-height:22px"> //以下是处理代码,可以抽取出来<br style="line-height:22px"> System.out.println("title ------------- authors ");<br style="line-height:22px"> for (int i = 0; i &lt; lBeans.size(); i++) {<br style="line-height:22px"> Book vals = (Book) lBeans.get(i); <br style="line-height:22px"> System.out.println(vals.getTitle ()+"-------------"+ vals. getAuthors ());<br style="line-height:22px"> }<br style="line-height:22px"> } catch (SQLException ex) {<br style="line-height:22px"> ex.printStackTrace();<br style="line-height:22px"> } finally {<br style="line-height:22px"> DbUtils.closeQuietly(conn);<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"> }<br style="line-height:22px"> 这两种法输出的结果应该是一样的。两种处理方式都差不多,但我更愿意采用第一种,因为第一种少写一个bean,而且我测试过采用Map的方式即第一种方式性能要好的多,采用Bean性能比较低可能是因为采用反射的缘故,采用反射的东东性能和不采用反射的还是有点差距。也是这个原因,不推荐采用LazyDynaClass和LazyDynaBean,因为采用这二者是在运行期动态创建Bean类和Bean属性,然后再创建Bean对象的,其性能可想而知了(不过我没有测试过啊,所以我说这个话可说是没有根据的,感兴趣的朋友自己测试一下,记得告诉我结果哦,呵呵),除了MapListHandler以及BeanListHandler之外,DButils还提供了其他的Handler,如果这些不能满足你的需求,你也可以自己实现一个Handler。<br style="line-height:22px"> 最后,也是最大的体会,也许是最大的收获吧,那就是:对于每一个项目,在根据每一个需求获取相应解决方案时,先寻找开源组件,看是否已经有满足某些功能需求的开源组件,如果没有,再考虑自主开发或者向第三方购买,否则尽量采用开源组件.<br style="line-height:22px"> 请尽量享用开源的魅力,尽情的拥抱开源吧。</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值