后台(21)——DBUtils
探索Android软键盘的疑难杂症 深入探讨Android异步精髓Handler 详解Android主流框架不可或缺的基石 站在源码的肩膀上全解Scroller工作机制 Android多分辨率适配框架(1)— 核心基础 Android多分辨率适配框架(2)— 原理剖析 Android多分辨率适配框架(3)— 使用指南 自定义View系列教程00–推翻自己和过往,重学自定义View 自定义View系列教程01–常用工具介绍 自定义View系列教程02–onMeasure源码详尽分析 自定义View系列教程03–onLayout源码详尽分析 自定义View系列教程04–Draw源码分析及其实践 自定义View系列教程05–示例分析 自定义View系列教程06–详解View的Touch事件处理 自定义View系列教程07–详解ViewGroup分发Touch事件 自定义View系列教程08–滑动冲突的产生及其处理 版权声明 本文原创作者:谷哥的小弟 作者博客地址:http://blog.csdn.net/lfdfhl DBUtils简介 DBUtils是Apache提供的一个开源的数据库操作工具,它是对JDBC的简单封装,极大简化JDBC编码的工作量。比如:查询数据时它可以把结果转换成List,Array,Set等集合,非常便于开发人员操作 DBUtils的三个核心对象 QueryRunner类 主要用于增,删,改,查 ResultSetHandler接口 主要用于处理结果集 DBUtils类 该工具类主要用于关闭连接、装载JDBC驱动程序等等 嗯哼,我们将在后续的篇幅中重点介绍QueryRunner类和ResultSetHandler接口,请继续往下看。 QueryRunner QueryRunner类提供了两个构造方法 new QueryRunner() 如果采用该方法创建QueryRunner,那么数据库的事务可由我们自己手动控制。正因为该构造方法无参,所以在调用该对象的query、update、batch时需要传入参数Connection。 new QueryRunner(DataSource ds); 如果采用该方法创建QueryRunner,那么数据库的事务由DBUtils自动控制。正因为该构造方法传入了参数QueryRunner,所以在调用该对象的query、update、batch时无需传入参数Connection。 QueryRunner的常用方法如下: public Object query(Connection conn, String sql, Object[] params, ResultSetHandler rsh) throws SQLException 执行查询操作,在该查询中Object数组里的每个值被用来作为查询语句的置换参数。该方法会自行处理PreparedStatement和ResultSet的创建和关闭。 public Object query(String sql, Object[] params, ResultSetHandler rsh) throws SQLException 该方法与上面的这个方法基本一样;不同的是它不用将Connection提供给该方法 public Object query(Connection conn, String sql, ResultSetHandler rsh) throws SQLException 该方法与上面的两个方法基本一样,它用于执行一个不需要置换参数的查询操作。 public int update(Connection conn, String sql, Object[] params) throws SQLException 执行更新操作,在该查询中Object数组里的每个元素值被用来作为更新语句的置换参数。 public int update(Connection conn, String sql) throws SQLException 该方法与上面的这个方法基本一样,用来执行一个不需要置换参数的更新操作。 现在,我们通过一个例子来了解DBUtils的简单使用 /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.sql.SQLException; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.junit.Test; public class TestCRUD { @Test public void testInsert() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); qr.update("insert into student(studentid,studentname) values(?,?)", 5,"吉泽暗步"); } @Test public void testUpdate() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); qr.update("update student set studentname=? where studentid=?", "明步吉泽",5); } @Test public void testDelete() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); qr.update("delete from student where studentid=?",2); } @Test public void testSelect()throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); BeanListHandler<Student> beanListHandler=new BeanListHandler<Student>(Student.class); List<Student> list = qr.query("select * from student",beanListHandler); for(Student s: list){ System.out.println(s); } } } 嗯哼,看到了吧,我们只用了很少的代码就实现了增删改查。尤其是最后一个例子中把查询结果直接转换成了List从而给开发带来了极大的方便! ResultSetHandler ResultSetHandler接口常用的实现类如下: ArrayHandler 把结果集中的第一行数据转成对象数组 ArrayListHandler 把结果集中的每一行数据都转成一个数组存放到List中 BeanHandler 将结果集中的第一行数据封装到一个对应的JavaBean实例中。 BeanListHandler 将结果集中的每一行数据都封装到一个对应的JavaBean实例中,存放到List里。 ColumnListHandler 将结果集中某一列的数据存放到List中 KeyedHandler(name) 将结果集中的每一行数据都封装到一个Map<列名,列值>里,再把这些map再存到一个map里,其key为指定的key。 MapHandler 将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值 MapListHandler 将结果集中的每一行数据都封装到一个Map里,然后再存放到List 关于ResultSetHandler接口实现类的用法,请看如下示例: /** * 本文作者:谷哥的小弟 * 博客地址:http://blog.csdn.net/lfdfhl */ package cn.com; import java.sql.SQLException; import java.util.List; import java.util.Map; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.ArrayHandler; import org.apache.commons.dbutils.handlers.ArrayListHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ColumnListHandler; import org.apache.commons.dbutils.handlers.KeyedHandler; import org.apache.commons.dbutils.handlers.MapHandler; import org.apache.commons.dbutils.handlers.MapListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import org.junit.Test; public class TestResultSetHandler { @Test public void teseArrayHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); Object[] arr = qr.query("select * from student", new ArrayHandler()); for (Object o : arr) { System.out.println(o); } } @Test public void teseArrayListHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); List<Object[]> query = qr.query("select * from student", new ArrayListHandler()); for (Object[] os : query) { System.out.println(os); for (Object o : os) { System.out.println(o); } System.out.println("----------"); } } @Test public void teseColumnListHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); List<Object> list = qr.query("select * from student", new ColumnListHandler(1)); for (Object o : list) { System.out.println(o); } } @Test public void testKeyedHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); Map<Object,Map<String,Object>> map = qr.query("select * from student", new KeyedHandler(1)); for (Map.Entry<Object, Map<String,Object>> m : map.entrySet()) { System.out.println(m.getKey()); for (Map.Entry<String, Object> mm : m.getValue().entrySet()) { System.out.println(mm.getKey()+"\t"+mm.getValue()); } System.out.println("----------"); } } @Test public void teseMapHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); Map<String,Object> map = qr.query("select * from student where studentid=?", new MapHandler(),3); for (Map.Entry<String, Object> m : map.entrySet()) { System.out.println(m.getKey()+"\t"+m.getValue()); } } @Test public void teseMapListHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); List<Map<String,Object>> list = qr.query("select * from student", new MapListHandler()); for (Map<String, Object> map : list) { for (Map.Entry<String, Object> m : map.entrySet()) { System.out.println(m.getKey()+"\t"+m.getValue()); } System.out.println("----------"); } } @Test public void teseScalarHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); Object o = qr.query("select count(*) from student", new ScalarHandler(1)); System.out.println(o.getClass().getName()); } @Test public void teseBeanHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); BeanHandler<Student> beanHandler=new BeanHandler<Student>(Student.class); Student student = qr.query("select * from student where studentid=?",beanHandler,3); System.out.println(student); } @Test public void teseBeanListHandler() throws SQLException{ QueryRunner qr = new QueryRunner(C3P0Util.getDataSource()); BeanListHandler<Student> beanListHandler=new BeanListHandler<Student>(Student.class); List<Student> list = qr.query("select * from student where studentid=?", beanListHandler,3); for(Student s:list){ System.out.println(s); } } } DBUtils DBUtils类提供了关闭连接、装载JDBC驱动程序等等于资源有关的操作。 DBUtils主要方法如下: public static void close( ) throws java.sql.SQLException 用于关闭Connection、Statement和ResultSet public static void closeQuietly( ): 这一类方法不仅能关闭Connection、Statement和ResultSet,还能隐藏一些在程序中抛出的SQLException。如果不想捕捉这些异常的话,这将是一个不错的选择 public static void commitAndCloseQuietly(Connection conn): 提交并关闭连接,并且在关闭连接时不抛出SQL异常。 public static boolean loadDriver(java.lang.String driverClassName) 装载并注册JDBC驱动程序,若成功就返回true