之前记录了纯 Java 版本的 JDBC 操作封装(https://www.cnblogs.com/itfky/p/13734227.html),今天记录下使用 DBUtils 中的 QueryRunner 和 c3p0 数据源来封装自用的 JDBC 辅助类,代码见下,支持批量和单个操作。需要注意的是,JDBC 底层在批量操作时, 对于不带参数的 SQL 语句,Statement.addBatch() 方法不会执行,这样存在执行后无返回的bug(详见《纯Java版本的JDBC基础操作,支持查询结果到泛型实体类的转换》中的注释),所以要区分是否要做批量操作。
package com.ldj.jdbc; /* * 使用 C3P0 数据源和 Apache 的 DBUtils 的JDBC操作 * * author: laideju itfky@foxmail.com * version: 1.0 * date: 2018-11-02 * */ import java.sql.Connection; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.handlers.BeanListHandler; import org.apache.commons.dbutils.handlers.ScalarHandler; import com.mchange.v2.c3p0.ComboPooledDataSource; public class DataSourceJdbcHelper { private static ComboPooledDataSource dataSource = new ComboPooledDataSource("mysql"); public static Connection getConnection() { Connection c = null; try { c= dataSource.getConnection(); } catch (Exception e) { e.printStackTrace(); } return c; } public static void close(ResultSet rs, Statement stat, Connection conn) { if(rs!=null) { try { rs.close(); rs = null; } catch (Exception e) { e.printStackTrace(); } } if(stat != null) { try { stat.close(); stat = null; } catch (Exception e) { e.printStackTrace(); } } if(conn != null) { try { conn.close(); conn = null; } catch (Exception e) { e.printStackTrace(); } } } /* * 通过 BeanListHandler 和 QueryRunner 获取对象 */ public static<T> List<T> queryResult(String sql, Object[] args, Class<T> clazz){ Connection conn = null; List<T> list = new ArrayList<T>(); try { conn=getConnection(); QueryRunner qr = new QueryRunner(); BeanListHandler<T> handler = new BeanListHandler<>(clazz); list = qr.query(conn, sql, handler, args); } catch (Exception e) { e.printStackTrace(); } return list; } /* * 获取统计函数的查询结果 */ public static Number getSimpleResult(String sql, Object[] params) { Number ret = -1; try { Connection conn=getConnection(); ScalarHandler<Number> rsh = new ScalarHandler<>(); if(params == null) { params = new Object[0]; } QueryRunner qr = new QueryRunner(); ret = qr.query(conn, sql, rsh, params); } catch (Exception e) { e.printStackTrace(); } return ret; } /* * 批量执行 insert 、 delete 、 update * 必须带参,否则 QueryRunner 的batch方法中不会执行 addBatch() 导致无法有效执行 */ public static int batchExecute(String sql, Object[][] params) { int ret = -1; Connection conn = null; try { conn = getConnection(); conn.setAutoCommit(true); QueryRunner qr = new QueryRunner(); if(params == null) { params = new Object[0][0]; } int[] rowNumList = qr.batch(conn, sql, params); if(rowNumList != null) { for(int i=0; i<rowNumList.length; i++) { ret += rowNumList[i]; } } } catch (Exception e) { e.printStackTrace(); } return ret; } /* * 执行单个的 insert 、 delete 、 update */ public static int execute(String sql, Object[] params) { int ret = -1; try { Connection conn = getConnection(); QueryRunner qr = new QueryRunner(); ret = qr.update(conn, sql, params); } catch (Exception e) { e.printStackTrace(); } return ret; } }