本例展現的是使用ResultSetHandler接口的幾個常見實現類實現數據庫的增刪改查,能夠大大減小代碼量,優化程序。java
ResultSetHandler的各個實現類:sql
(一)首先,創建一個與數據庫emp表相對應的一個實體類Emp,Emp類代碼以下:數據庫
import java.util.Date; public class Emp { private Integer empno; private String ename; private String job; private Integer mgr; private Date hiredate; private Double sal; private Double comm; private Integer deptno; public Emp() { super(); } public Emp(Integer empno, String ename, String job, Integer mgr, Date hiredate, Double sal, Double comm, Integer deptno) { super(); this.empno = empno; this.ename = ename; this.job = job; this.mgr = mgr; this.hiredate = hiredate; this.sal = sal; this.comm = comm; this.deptno = deptno; } public Integer getEmpno() { return empno; } public void setEmpno(Integer empno) { this.empno = empno; } public String getEname() { return ename; } public void setEname(String ename) { this.ename = ename; } public String getJob() { return job; } public void setJob(String job) { this.job = job; } public Integer getMgr() { return mgr; } public void setMgr(Integer mgr) { this.mgr = mgr; } public Date getHiredate() { return hiredate; } public void setHiredate(Date hiredate) { this.hiredate = hiredate; } public Double getSal() { return sal; } public void setSal(Double sal) { this.sal = sal; } public Double getComm() { return comm; } public void setComm(Double comm) { this.comm = comm; } public Integer getDeptno() { return deptno; } public void setDeptno(Integer deptno) { this.deptno = deptno; } @Override public String toString() { return "Emp [empno=" + empno + ", ename=" + ename + ", job=" + job + ", mgr=" + mgr + ", hiredate=" + hiredate + ", sal=" + sal + ", comm=" + comm + ", deptno=" + deptno + "]"; } }
(二)爲方便鏈接數據庫,建立一個工具類JdbcUtils,類中代碼以下:apache
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import org.apache.commons.dbutils.DbUtils; public class JdbcUtils { //用於鏈接數據庫的靜態常量 private static String URL = "jdbc:oracle:thin:@localhost:1521:ORCL"; private static String USER = "scott"; private static String PASSWORD = "tiger"; //使用靜態塊加載驅動 static { DbUtils.loadDriver("oracle.jdbc.driver.OracleDriver"); } /** * 得到鏈接的靜態方法 * @return */ public static Connection getConnection() { try { return (Connection) DriverManager.getConnection(URL, USER, PASSWORD); } catch (SQLException e) { System.out.println("得到數據鏈接失敗:" + e.getMessage()); } return null; } /** * 編寫程序過程當中可以使用main測試是否能夠得到鏈接對象 */ public static void main(String[] args) { System.out.println(getConnection()); } }
(三)接下來建立一個JUnit測試類EmpCRUDTest,是對ResultSetHandler接口的幾個常見實現類實現數據庫的增刪改查操做測試,代碼以下:數組
import static org.junit.Assert.*; import java.math.BigDecimal; import java.sql.Connection; import java.sql.SQLException; import java.util.Date; import java.util.List; import java.util.Map; import org.apache.commons.dbutils.DbUtils; import org.apache.commons.dbutils.QueryRunner; import org.apache.commons.dbutils.ResultSetHandler; import org.apache.commons.dbutils.handlers.BeanHandler; import org.apache.commons.dbutils.handlers.BeanListHandler; 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 EmpCRUDTest { // 全部CRUD的操做都經過此類完成 QueryRunner runner = new QueryRunner(); /** * BeanListHandler類測試 * BeanListHandler:將結果集中的每一行數據都封裝到一個對應的JavaBean實例中,存放到List裏。 */ @Test public void testSelect() { //調用工具類JdbcUtils的getConnection()方法得到鏈接 Connection conn = JdbcUtils.getConnection(); String sql = "select * from emp"; //建立BeanListHandler類實例 /*ResultSetHandler<List<Emp>> rsh = new BeanListHandler<Emp>(Emp.class);*/ ResultSetHandler<List<Emp>> rsh = new BeanListHandler<Emp>(Emp.class); try { //執行查詢操做 List<Emp> list = runner.query(conn, sql, rsh); //遍歷list查看返回結果 for (Emp e : list) { System.out.println(e); } } catch (SQLException e) { e.printStackTrace(); } finally { //關閉流 DbUtils.closeQuietly(conn); } } /** * BeanListHandler類有參數的狀況測試 查詢多個員工信息 * */ @Test public void testSelect2() { Connection conn = JdbcUtils.getConnection(); String sql = "select * from emp where sal between ? and ? and ename like ?"; ResultSetHandler<List<Emp>> rsh = new BeanListHandler<Emp>(Emp.class); try { // 爲每一個問號賦一個值綁定變量 List<Emp> list = runner.query(conn, sql, rsh, 1.0, 8888.0, "%A%"); for (Emp e : list) { System.out.println(e); } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } } /** * BeanHandler類測試 * BeanHandler:將結果集中的第一行數據封裝到一個對應的JavaBean實例中。適用於查詢結果只有一行的狀況 */ @Test public void testSelect3() { Connection conn = JdbcUtils.getConnection(); //建立BeanHandler實例 BeanHandler<Emp> rsh = new BeanHandler<Emp>(Emp.class); String sql = "select * from emp where empno=?";//查詢語句只會一行結果 try { //執行查詢 Emp e = runner.query(conn, sql, rsh, 7698); //打印輸出查看結果 System.out.println(e); } catch (SQLException e) { e.printStackTrace(); } finally { //關閉鏈接 DbUtils.closeQuietly(conn); } } /** * MapHandler操做結果集測試 查詢成java.util.Map, Map<列名,值> 把結果集轉爲一個 Map 對象, 並返回. * 若結果集中有多條記錄, 僅返回第一條記錄對應的 Map 對象. Map 的鍵: 列名(而非列的別名), 值: 列的值 */ @Test public void testSelectForMap() { Connection conn = JdbcUtils.getConnection(); //建立MapHandler類實例 MapHandler mapHandler = new MapHandler(); //該查詢語句會返回一條記錄 String sql = "select * from emp where empno=?"; try { //執行SQl語句,記錄的列名將做爲map集合的key,列名對應的值則是map集合的value Map<String, Object> value = runner.query(conn, sql, mapHandler, 8888); //根據map的key獲取對應的value,打印查看結果 System.out.println(value.get("EMPNO") + "," + value.get("ENAME") + "," + value.get("JOB") + "," + value.get("MGR") + "," + value.get("HIREDATE") + "," + value.get("SAL")); } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } } /** * MapListHandler操做結果集測試 MapListHandler 類 (實現ResultSetHandler 接口)把從數據庫中查詢出的記錄都 * 放到List集合當中, List集合中每個對象都是Map類型,能夠根據這條記錄的字段名讀出相對應的值. */ @Test public void testSelectForMapList() { Connection conn = JdbcUtils.getConnection(); //建立MapListHandler類實例 MapListHandler mapListHandler = new MapListHandler(); String sql = "select * from emp"; try { //執行SQL語句,會返回多條記錄,每一條記錄都保存在一個map集合中,而全部的集合都放在一個list中 List<Map<String, Object>> query = runner.query(conn, sql, mapListHandler); //遍歷輸出,查看結果 for (Map<String, Object> value : query) { System.out.println(value.get("EMPNO") + "," + value.get("ENAME") + "," + value.get("JOB") + "," + value.get("MGR") + "," + value.get("HIREDATE") + "," + value.get("SAL")); } } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } } /** * 標量查詢 * ScalarHandler:能夠返回指定列的一個值或返回一個統計函數的值 */ @Test public void testSelectForScalar() { Connection conn = JdbcUtils.getConnection(); //SQl語句,爲計數列起一個別名count String sql = "select count(*) count from emp"; //建立ScalarHandler類實例,傳入的參數的列名, 返回大數據類型的結果 ScalarHandler<BigDecimal> scalarHandler = new ScalarHandler<BigDecimal>("count"); try { //執行SQl語句, 返回值是大數據類型的值 BigDecimal rows = runner.query(conn, sql, scalarHandler); System.out.println("共查詢到了:"+rows.intValue()+"條記錄");// 將rows轉成int型 } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } } /** * 插入數據測試 */ @Test public void testInsert() { Connection conn = JdbcUtils.getConnection(); java.sql.Date date = new java.sql.Date(new Date().getTime());// date做爲時間插入數據庫表中 String sql = "insert into emp(empno,ename,job,hiredate,sal,deptno) " + "values(?,?,?,?,?,?)"; try { //執行插入語句 int rows = runner.update(conn, sql, 8888, "JASSICA", "SALESMAN", date, 3344.0, 10); System.out.println("插入了:" + rows + "行"); } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } } /** * 更新數據測試 */ @Test public void testUpdate() { Connection conn = JdbcUtils.getConnection(); String sql = "update emp set sal=?,deptno=? where empno=?"; try { int rows = runner.update(conn, sql, 4455, 20, 8888); System.out.println("更新了:" + rows + "行"); } catch (SQLException e) { e.printStackTrace(); } finally { DbUtils.closeQuietly(conn); } } }
剛剛學習,如看法有誤歡迎指正!oracle