1. 程式人生 > 其它 >JDBC 筆記09(DBUtils)

JDBC 筆記09(DBUtils)

技術標籤:jdbc資料庫jdbcjavamysql資料庫

一、Apache-DBUtils 簡介

  • commons-dbutils 是Apache組織提供的一個開源JDBC工具類庫,它是對JDBC的簡單封裝,學習成本極低,並且使用dbutils能極大簡化 jdbc 編碼的工作量,同時也不會影響程式的效能。
  • API介紹:
    org.apache.commons.dbutils.QueryRunner
    org.apache.commons.dbutils.ResultSetHandler
    工具類: org.apache.commons.dbutils.DbUtils

二、使用

需要匯入 DBUtils 的一個jar包

1、改(增刪 類似)

    @Test
    public void testUpdate() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnectionFromDruid();
            String sql = "update customers set cust_email=? where cust_id = ?";

            QueryRunner runner = new QueryRunner
(); int updateCount = runner.update(connection, sql, "[email protected]", 10002); System.out.println("修改了" + updateCount + "條記錄。"); } catch (Exception e) { e.printStackTrace(); } finally { JDBCUtils.closeResoure
(null, connection); } }

2、查(一條)

    @Test
    /**
     * BeanHandler:是ResultSetHandler介面的實現類,用於封裝表中的一條記錄
     */
    public void testQuery01() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnectionFromDruid();
            String sql = "select cust_id custId,cust_name custName,cust_email custEmail from customers where cust_id = ?";

            BeanHandler<Customers> beanHandler = new BeanHandler<>(Customers.class);

            QueryRunner runner = new QueryRunner();
            Customers customers = runner.query(connection, sql, beanHandler, 10005);
            System.out.println(customers);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResoure(null, connection);
        }
    }

3、查(多條)

    @Test
    /**
     * BeanListHandler:是ResultSetHandler介面的實現類,用於封裝表中的多條記錄構成的的集合
     */
    public void testQuery02() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnectionFromDruid();
            String sql = "select cust_id custId,cust_name custName,cust_email custEmail from customers where cust_id < ?";

            BeanListHandler<Customers> handler = new BeanListHandler<>(Customers.class);
            QueryRunner runner = new QueryRunner();

            List<Customers> customersList = runner.query(connection, sql, handler, 10005);
            customersList.forEach(System.out::println);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResoure(null, connection);
        }
    }

4、查(特殊值)

    @Test
    /**
     * ScalarHandler:用於查詢特殊值
     */
    public void testQuery03() {
        Connection connection = null;
        try {
            connection = JDBCUtils.getConnectionFromDruid();
            String sql = "select count(*) from customers";

            ScalarHandler handler = new ScalarHandler();
            QueryRunner runner = new QueryRunner();

            Long count = (Long) runner.query(connection, sql, handler);
            System.out.println(count);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtils.closeResoure(null, connection);
        }
    }

5、在 BaseDAO 中使用

以下為 update 方法:

還有其他的方法沒有寫出,可自行寫入。

    /**
     * 直接使用DBUtils的update方法
     * @param connection
     * @param sql
     * @param args
     */
    public void updateByDBUtils(Connection connection, String sql, Object ...args) {
        QueryRunner runner = new QueryRunner();
        try {
            runner.update(connection, sql, args);
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
    }

三、使用DBUtils關閉資源

可直接在類 JDBCUtils 中新增以下程式碼:

    /**
     * 使用了DBUtils裡面的方法
     * 關閉 Statement 和 connection 的方法
     * @param Statement
     * @param connection
     */
    public static void closeResoureByDBUtils(Statement Statement, Connection connection) {
        DbUtils.closeQuietly(Statement);
        DbUtils.closeQuietly(connection);
    }

    /**
     * 使用了DBUtils裡面的方法
     * 關閉 Statement 和 connection 和 resultSet 的方法
     * @param Statement
     * @param connection
     * @param resultSet
     */
    public static void closeResoureByDBUtils(Statement Statement, Connection connection, ResultSet resultSet) {
        DbUtils.closeQuietly(Statement);
        DbUtils.closeQuietly(connection);
        DbUtils.closeQuietly(resultSet);
    }