commons-dbutils 是 Apache 組織提供的一個開源 JDBC工具類庫,它是對JDBC的簡單封裝,學習成本極低,而且使用dbutils能極大簡化jdbc編碼的工做量,同時也不會影響程序的性能。java
API包說明:sql
該類簡單化了SQL查詢,它與ResultSetHandler組合在一塊兒使用能夠完成大部分的數據庫操做,可以大大減小編碼量。數據庫
測試apache
// 測試添加 @Test public void testInsert() throws Exception { QueryRunner runner = new QueryRunner(); Connection conn = JDBCUtils.getConnection3(); String sql = "insert into customers(name,email,birth)values(?,?,?)"; int count = runner.update(conn, sql, "何成飛", "he@qq.com", "1992-09-08"); System.out.println("添加了" + count + "條記錄"); JDBCUtils.closeResource(conn, null); }
// 測試刪除 @Test public void testDelete() throws Exception { QueryRunner runner = new QueryRunner(); Connection conn = JDBCUtils.getConnection3(); String sql = "delete from customers where id < ?"; int count = runner.update(conn, sql,3); System.out.println("刪除了" + count + "條記錄"); JDBCUtils.closeResource(conn, null); }
該接口用於處理 java.sql.ResultSet,將數據按要求轉換爲另外一種形式。api
ResultSetHandler 接口提供了一個單獨的方法:Object handle (java.sql.ResultSet .rs)。數組
接口的主要實現類:ide
測試工具
/* * 測試查詢:查詢一條記錄 * * 使用ResultSetHandler的實現類:BeanHandler */ @Test public void testQueryInstance() throws Exception{ QueryRunner runner = new QueryRunner(); Connection conn = JDBCUtils.getConnection3(); String sql = "select id,name,email,birth from customers where id = ?"; // BeanHandler<Customer> handler = new BeanHandler<>(Customer.class); Customer customer = runner.query(conn, sql, handler, 23); System.out.println(customer); JDBCUtils.closeResource(conn, null); }
/* * 測試查詢:查詢多條記錄構成的集合 * * 使用ResultSetHandler的實現類:BeanListHandler */ @Test public void testQueryList() throws Exception{ QueryRunner runner = new QueryRunner(); Connection conn = JDBCUtils.getConnection3(); String sql = "select id,name,email,birth from customers where id < ?"; // BeanListHandler<Customer> handler = new BeanListHandler<>(Customer.class); List<Customer> list = runner.query(conn, sql, handler, 23); list.forEach(System.out::println); JDBCUtils.closeResource(conn, null); }
/* * 自定義ResultSetHandler的實現類 */ @Test public void testQueryInstance1() throws Exception{ QueryRunner runner = new QueryRunner(); Connection conn = JDBCUtils.getConnection3(); String sql = "select id,name,email,birth from customers where id = ?"; ResultSetHandler<Customer> handler = new ResultSetHandler<Customer>() { @Override public Customer handle(ResultSet rs) throws SQLException { System.out.println("handle"); // return new Customer(1,"Tom","tom@126.com",new Date(123323432L)); if(rs.next()){ int id = rs.getInt("id"); String name = rs.getString("name"); String email = rs.getString("email"); Date birth = rs.getDate("birth"); return new Customer(id, name, email, birth); } return null; } }; Customer customer = runner.query(conn, sql, handler, 23); System.out.println(customer); JDBCUtils.closeResource(conn, null); }
/* * 如何查詢相似於最大的,最小的,平均的,總和,個數相關的數據, * 使用ScalarHandler * */ @Test public void testQueryValue() throws Exception{ QueryRunner runner = new QueryRunner(); Connection conn = JDBCUtils.getConnection3(); //測試一: // String sql = "select count(*) from customers where id < ?"; // ScalarHandler handler = new ScalarHandler(); // long count = (long) runner.query(conn, sql, handler, 20); // System.out.println(count); //測試二: String sql = "select max(birth) from customers"; ScalarHandler handler = new ScalarHandler(); Date birth = (Date) runner.query(conn, sql, handler); System.out.println(birth); JDBCUtils.closeResource(conn, null); }