DBUtils框架简化JDBC开发

本文介绍commons-dbutils,一个简化JDBC操作的工具包。通过示例演示了如何使用它进行数据库的增删改查及批处理操作,并展示了不同的结果集处理器用法。

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

commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用dbutils能极大简化jdbc编码的工作量,同时也不会影响程序的性能。因此dbutils成为很多不喜欢hibernate的公司的首选。

下面是DBUtils对数据的crud示例:

package cn.test.jdbc; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; import cn.itcast.Utils.JdbcUtils; import cn.itcast.domain.Account; public class Demo1 { @Test public void add() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource());//从连接池中获取连接 String sql ="insert into account(name,money) values(?,?)"; Object params[]={"ddd",1000}; qr.update(sql, params); } @Test public void delete() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="delete from account where id=?"; qr.update(sql, 85); } @Test public void update() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="update account set money=? where id=?"; Object params[]={10,3}; qr.update(sql,params); } @Test public void find() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="select * from account where id=?"; //将查询到的结果封装到实体中 Account a=(Account) qr.query(sql,66, new BeanHandler(Account.class)); System.out.println(a.getMoney()); } @Test public void getAll() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="select * from account"; //将查询到的结果封装到集合中 List list=(List) qr.query(sql, new BeanListHandler(Account.class)); System.out.println(list); } //批处理 @Test public void batch() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="insert into account(name,money) values(?,?)"; Object parame[][]=new Object [20][]; for(int i=0;i<20;i++){ parame[i]=new Object[]{"aa"+i,1000}; } qr.batch(sql, parame); } }



测试各个结果集处理器:

@Test public void test1() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="select * from account"; //获取结果集的第一条数据 Object result []=(Object[])qr.query(sql,new ArrayHandler()); System.out.println(result); } @Test public void test2() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="select * from account"; //获取name列的所有集合 List result =(List)qr.query(sql,new ColumnListHandler("name")); System.out.println(result); } @Test public void test3() throws Exception{ QueryRunner qr =new QueryRunner(JdbcUtils.getDataSource()); String sql ="select * from account"; //将结果集中的每一行数据都封装一个map中,再将这些map存到一个map中,其key是列名 Map<Integer,Map<String,Object>> map =(Map) qr.query(sql,new KeyedHandler("id")); Set<Entry<Integer, Map<String, Object>>> set=map.entrySet(); for(Map.Entry<Integer,Map<String,Object>> entry:set){ Set<Entry<String, Object>> innerset=entry.getValue().entrySet(); for(Map.Entry<String,Object>innerse:innerset){ System.out.println(innerse.getKey()+"="+innerse.getValue()); } } }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值