【转载】关于DBUtils中QueryRunner的一些解读
前面已经有文章说了DBUtils的一些特性, 这里再来详细说下QueryRunner的一些内部实现, 写的有错误的地方还恳请大家指出.
QueryRunner类
QueryRunner中提供对sql语句操作的API
它主要有三个方法
query() 用于执行select
update() 用于执行insert/update/delete
batch() 批处理
1,Query语句
先来看下query的两种形式, 我们这里主要讲第一个方法, 因为我们用C3P0来统一管理connection.(QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()))
query(sql,ResultSetHandler,Object...params);
query(conn,sql,ResultSetHandler,Object...params);
第一种: 不需要params
- //查询所有图书
- public List<Book> selectAllBooks() throws SQLException {
- QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
- return qr.query("select * from books", new BeanListHandler<Book>(Book.class));
- }
第二种: 需要一个参数查询
- //根据id查询指定的书
- public Book selectBookById(String id) throws SQLException {
- QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
- return qr.query("select * from books where id=?", new BeanHandler(Book.class),id);
- }
第三种:需要多个参数查询

- //多条件查询图书信息
- public List<Book> findBookByManyCondition(String id, String category,
- String name, String minprice, String maxprice) throws SQLException {
- StringBuilder sql = new StringBuilder("select * from books where 1=1");
- List list = new ArrayList();
- if(!"".equals(id)){
- sql.append(" and id like ?");
- list.add("%"+id+"%");
- }
- if(!"".equals(category)){
- sql.append(" and category=?");
- list.add(category);
- }
- if(!"".equals(name)){
- sql.append(" and name like ?");
- list.add("%"+name+"%");
- }
- if(!"".equals(minprice)){
- sql.append(" and price > ?");
- list.add(minprice);
- }
- if(!"".equals(maxprice)){
- sql.append(" and price < ?");
- list.add(maxprice);
- }
- QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
- return qr.query(sql.toString(),new BeanListHandler<Book>(Book.class),list.toArray());
- }

那么我们来看下源码的实现:
(1)QueryRunner.java

- //第一种情况,无参数
- public <T> T query(String sql, ResultSetHandler<T> rsh) throws SQLException {
- Connection conn = this.prepareConnection();
- return this.query(conn, true, sql, rsh, (Object[]) null);
- }
- //第二种和第三种使用同一方法: 需要参数
- public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params) throws SQLException {
- Connection conn = this.prepareConnection();
- return this.query(conn, true, sql, rsh, params);
- }

解读: 这里先是获取connection, 利用this.preparaConnection() 获取. 然后调用query()方法去执行查询语句. 接下来看源码是如何获取到当前传输过来的connection以及query()方法的内部实现.

- protected Connection prepareConnection() throws SQLException {
- if (this.getDataSource() == null) {
- throw new SQLException("QueryRunner requires a DataSource to be " +
- "invoked in this way, or a Connection should be passed in");
- }
- return this.getDataSource().getConnection();
- }

这里很简单, 因为我们用的C3P0数据库连接池获取的DataSource, 所以这里直就可以过去到当前的Connection.接下来就看下query()方法的内部实现.

- private <T> T query(Connection conn, boolean closeConn, String sql, ResultSetHandler<T> rsh, Object... params)
- throws SQLException {
- if (conn == null) {
- throw new SQLException("Null connection");
- }
- if (sql == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null SQL statement");
- }
- if (rsh == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null ResultSetHandler");
- }
- PreparedStatement stmt = null;
- ResultSet rs = null;
- T result = null;
- try {
- stmt = this.prepareStatement(conn, sql);
- this.fillStatement(stmt, params);
- rs = this.wrap(stmt.executeQuery());
- result = rsh.handle(rs);
- } catch (SQLException e) {
- this.rethrow(e, sql, params);
- } finally {
- try {
- close(rs);
- } finally {
- close(stmt);
- if (closeConn) {
- close(conn);
- }
- }
- }
- return result;
- }

解读: 在这里可以看出, 无论是否有传递参数params, 都调用的是同一个query方法, 接着来看this.fillStatement(stmt, params);是如何将参数赋予preparedStatement中的.

- public void fillStatement(PreparedStatement stmt, Object... params) throws SQLException {
- // check the parameter count, if we can
- ParameterMetaData pmd = null;
- if (!pmdKnownBroken) {
- pmd = stmt.getParameterMetaData();
- int stmtCount = pmd.getParameterCount();
- int paramsCount = params == null ? 0 : params.length;
- if (stmtCount != paramsCount) {
- throw new SQLException("Wrong number of parameters: expected "
- + stmtCount + ", was given " + paramsCount);
- }
- }
- // nothing to do here
- if (params == null) {
- return;
- }
- for (int i = 0; i < params.length; i++) {
- if (params[i] != null) {
- stmt.setObject(i + 1, params[i]);
- } else {
- // VARCHAR works with many drivers regardless
- // of the actual column type. Oddly, NULL and
- // OTHER don't work with Oracle's drivers.
- int sqlType = Types.VARCHAR;
- if (!pmdKnownBroken) {
- try {
- sqlType = pmd.getParameterType(i + 1);
- } catch (SQLException e) {
- pmdKnownBroken = true;
- }
- }
- stmt.setNull(i + 1, sqlType);
- }
- }
- }

这个方法就是核心所在.
第一种情况: 当params为null的时候, 直接return然后执行sql语句.
第二种第三种情况: 当params不为null时, 循环遍历传入的params, 然后将params赋值到preparedStatement中, 然后填充占位符进行sql查询. 这里我们也来回顾下直接使用preparedStatement来进行查询的方式:

- @Test
- public void update(){
- Connection conn = null;
- PreparedStatement st = null;
- ResultSet rs = null;
- try{
- conn = JdbcUtils.getConnection();
- String sql = "update users set name=?,email=? where id=?";
- st = conn.prepareStatement(sql);
- st.setString(1, "gacl");
- st.setString(2, "gacl@sina.com");
- st.setInt(3, 2);
- int num = st.executeUpdate();
- if(num>0){
- System.out.println("更新成功!!");
- }
- }catch (Exception e) {
- e.printStackTrace();
- }finally{
- JdbcUtils.release(conn, st, rs);
- }
- }
- @Test
- public void find(){
- Connection conn = null;
- PreparedStatement st = null;
- ResultSet rs = null;
- try{
- conn = JdbcUtils.getConnection();
- String sql = "select * from users where id=?";
- st = conn.prepareStatement(sql);
- st.setInt(1, 1);
- rs = st.executeQuery();
- if(rs.next()){
- System.out.println(rs.getString("name"));
- }
- }catch (Exception e) {
- }finally{
- JdbcUtils.release(conn, st, rs);
- }
- }

2, Update语句
查看update语句:

- //修改图书
- public void updateBook(Book book) throws SQLException {
- QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
- qr.update(
- "UPDATE books SET NAME=? ,price=?,bnum=?,category=?,description=? WHERE id=?",
- book.getName(), book.getPrice(), book.getBnum(),
- book.getCategory(), book.getDescription(), book.getId())
- }

接着是QueryRunner.java中的update 方法:

- public int update(String sql, Object... params) throws SQLException {
- Connection conn = this.prepareConnection();
- return this.update(conn, true, sql, params);
- }
- private int update(Connection conn, boolean closeConn, String sql, Object... params) throws SQLException {
- if (conn == null) {
- throw new SQLException("Null connection");
- }
- if (sql == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null SQL statement");
- }
- PreparedStatement stmt = null;
- int rows = 0;
- try {
- stmt = this.prepareStatement(conn, sql);
- this.fillStatement(stmt, params);
- rows = stmt.executeUpdate();
- } catch (SQLException e) {
- this.rethrow(e, sql, params);
- } finally {
- close(stmt);
- if (closeConn) {
- close(conn);
- }
- }
- return rows;
- }

到了参数赋值的时候又调用了上面的fillStatement方法, 这里就不再阐述了.
3, Batch语句
这里直接看batch方法的实例, 然后结合源码的实现.

- //批量删除
- public void delBooks(String[] ids) throws SQLException {
- QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource());
- Object[][] params = new Object[ids.length][];//高维确定执行sql语句的次数,低维是给?赋值
- for (int i = 0; i < params.length; i++) {
- params[i] = new Object[]{ids[i]};//给“?”赋值
- }
- qr.batch("delete from books where id=?", params);
- }

然后看QueryRunner中的batch()方法:

- public int[] batch(String sql, Object[][] params) throws SQLException {
- Connection conn = this.prepareConnection();
- return this.batch(conn, true, sql, params);
- }
- private int[] batch(Connection conn, boolean closeConn, String sql, Object[][] params) throws SQLException {
- if (conn == null) {
- throw new SQLException("Null connection");
- }
- if (sql == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null SQL statement");
- }
- if (params == null) {
- if (closeConn) {
- close(conn);
- }
- throw new SQLException("Null parameters. If parameters aren't need, pass an empty array.");
- }
- PreparedStatement stmt = null;
- int[] rows = null;
- try {
- stmt = this.prepareStatement(conn, sql);
- for (int i = 0; i < params.length; i++) {
- this.fillStatement(stmt, params[i]);
- stmt.addBatch();
- }
- rows = stmt.executeBatch();
- } catch (SQLException e) {
- this.rethrow(e, sql, (Object[])params);
- } finally {
- close(stmt);
- if (closeConn) {
- close(conn);
- }
- }
- return rows;
- }

解读: 因为params是一个二维数组, 所以往preparedStatement中赋值的时候使用了for循环, 然后通过preparedstatement.addBatch() 进行批量添加, 然后执行executeBatch()进行操作.

- /**
- * Adds a set of parameters to this <code>PreparedStatement</code>
- * object's batch of commands.
- *
- * @exception SQLException if a database access error occurs or
- * this method is called on a closed <code>PreparedStatement</code>
- * @see Statement#addBatch
- * @since 1.2
- */
- void addBatch() throws SQLException;

转载自一枝花算不算浪漫
https://www.cnblogs.com/wang-meng/p/5525389.html
【转载】关于DBUtils中QueryRunner的一些解读的更多相关文章
- [JavaWeb]关于DBUtils中QueryRunner的一些解读.
前言:[本文属于原创分享文章, 转载请注明出处, 谢谢.]前面已经有文章说了DBUtils的一些特性, 这里再来详细说下QueryRunner的一些内部实现, 写的有错误的地方还恳请大家指出. Que ...
- [JavaWeb]关于DBUtils中QueryRunner的一些解读(转)
QueryRunner类 QueryRunner中提供对sql语句操作的API它主要有三个方法 query() 用于执行select update() 用于执行insert/update/delete ...
- 关于dbutils中QueryRunner看批量删除语句batch
//批量删除 public void delBooks(String[] ids) throws SQLException { QueryRunner qr = new QueryRunner(C3P ...
- [转载]解析WINDOWS中的DLL文件---经典DLL解读
[转载]解析WINDOWS中的DLL文件---经典DLL解读 在Windows世界中,有无数块活动的大陆,它们都有一个共同的名字——动态链接库.现在就走进这些神奇的活动大陆,找出它们隐藏已久的秘密吧! ...
- java—在dbutils中处理事务与不确定条件的查询(46)
在dbutils中处理事务 事务是指用户的一次操作.这一次操作有可能是一个表,也有可能是多个表,也有可能是对一个表的多次操作. 只要是: 1:对数据数据库进行多次操作. 2:多个表,还是 ...
- 转载:WinForm中播放声音的三种方法
转载:WinForm中播放声音的三种方法 金刚 winForm 播放声音 本文是转载的文章.原文出处:http://blog.csdn.net/jijunwu/article/details/4753 ...
- 转载:C#中事件和委托的编译代码
接上文转载:C#中事件的由来,这时候,我们注释掉编译错误的行,然后重新进行编译,再借助Reflactor来对 event的声明语句做一探究,看看为什么会发生这样的错误: public event Gr ...
- 转载:C#中事件的由来
原文地址 http://www.tracefact.net/CSharp-Programming/Delegates-and-Events-in-CSharp.aspx 感谢博主分享! 我们继续思考转 ...
- 【转载】 Sqlserver中通过Select Into语句快速单表备份
在Sqlserver数据库中,备份数据的方式有很多种,可以使用整个数据库备份,也可使用导出包含数据和架构的脚本文件的方式来进行单表或多表数据的备份,其实还有一种Select Into的方式可以快速备份 ...
随机推荐
- java.lang.ClassFormatError: Unknown constant tag 0 in class file
在通过文件上传之后,运行java程序,突然发现这么一个错误:java.lang.ClassFormatError: Unknown constant tag 0 in class file,通过网上查 ...
- [bzoj2599][IOI2011]Race_树上点分治
Race bzoj-2599 题目大意:询问一颗树上最短的.长度为k的链,边有边权,n个节点. 注释:$1\le n \le 2\cdot 10^5$,$1\le k \le 10^6$. 想法:树上 ...
- POJ 3278 Catch That Cow(求助大佬)
Catch That Cow Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 109702 Accepted: 34255 ...
- Java分代垃圾回收机制:年轻代/年老代/持久代(转)
虚拟机中的共划分为三个代:年轻代(Young Generation).年老点(Old Generation)和持久代(Permanent Generation).其中持久代主要存放的是Java类的类信 ...
- PHP array_diff_ukey()
定义和用法 array_diff_ukey() 返回一个数组,该数组包括了所有出现在 array1 中但是未出现在任何其它参数数组中的键名的值.注意关联关系保留不变.与 array_diff() 不同 ...
- find -perm命令
http://www.2cto.com/os/201205/130125.html find -perm,根据文件的权限来查找文件,有三种形式: find -perm mode find -perm ...
- Yum重装走过的坑
今天因为用yum方式安装mongo遇到报错,从而我选择卸载yum并重新安装. 我先选择了用rpm方式进行重装,从163的packages列表里面找到64位redhat6.5可以用的三个rpm包,安装过 ...
- oc55--ARC单个对象的内存管理
// Person.h #import <Foundation/Foundation.h> @interface Person : NSObject @end // Person.m #i ...
- javascript之模块加载方案
前言 主要学习一下四种模块加载规范: AMD CMD CommonJS ES6 模块 历史 前端模块化开发那点历史 require.js requirejs 为全局添加了 define 函数,你只要按 ...
- 函数中的this的四种绑定形式
目录 this的默认绑定 this的隐式绑定 隐式绑定下,作为对象属性的函数,对于对象来说是独立的 在一串对象属性链中,this绑定的是最内层的对象 this的显式绑定:(call和bind方法) n ...