第八课 Apache-DBUtils实现CRUD操作

1 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
  • API包说明:

在这里插入图片描述

在这里插入图片描述

2 主要API的使用

2.1 DbUtils

  • DbUtils :提供如关闭连接、装载JDBC驱动程序等常规工作的工具类,里面的所有方法都是静态的。主要方法如下:
    • public static void close(…) throws java.sql.SQLException: DbUtils类提供了三个重载的关闭方法。这些方法检查所提供的参数是不是NULL,如果不是的话,它们就关闭Connection、Statement和ResultSet。
    • public static void closeQuietly(…): 这一类方法不仅能在Connection、Statement和ResultSet为NULL情况下避免关闭,还能隐藏一些在程序中抛出的SQLEeception。
    • public static void commitAndClose(Connection conn)throws SQLException: 用来提交连接的事务,然后关闭连接
    • public static void commitAndCloseQuietly(Connection conn): 用来提交连接,然后关闭连接,并且在关闭连接时不抛出SQL异常。
    • public static void rollback(Connection conn)throws SQLException:允许conn为null,因为方法内部做了判断
    • public static void rollbackAndClose(Connection conn)throws SQLException
    • rollbackAndCloseQuietly(Connection)
    • public static boolean loadDriver(java.lang.String driverClassName):这一方装载并注册JDBC驱动程序,如果成功就返回true。使用该方法,你不需要捕捉这个异常ClassNotFoundException。

2.2 QueryRunner类

  • 该类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量。

  • QueryRunner类提供了两个构造器:

    • 默认的构造器
    • 需要一个 javax.sql.DataSource 来作参数的构造器
  • QueryRunner类的主要方法:

    • 更新
      • public int update(Connection conn, String sql, Object... params) throws SQLException:用来执行一个更新(插入、更新或删除)操作。
    • 插入
      • public <T> T insert(Connection conn,String sql,ResultSetHandler<T> rsh, Object... params) throws SQLException:只支持INSERT语句,其中 rsh - The handler used to create the result object from the ResultSet of auto-generated keys. 返回值: An object generated by the handler.即自动生成的键值
    • 批处理
      • public int[] batch(Connection conn,String sql,Object[][] params)throws SQLException: INSERT, UPDATE, or DELETE语句
      • public <T> T insertBatch(Connection conn,String sql,ResultSetHandler<T> rsh,Object[][] params)throws SQLException:只支持INSERT语句
    • 查询
      • public Object query(Connection conn, String sql, ResultSetHandler rsh,Object... params) throws SQLException:执行一个查询操作,在这个查询中,对象数组中的每个元素值被用来作为查询语句的置换参数。该方法会自行处理 PreparedStatement 和 ResultSet 的创建和关闭。
package JDBC.QueryRunner;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;

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 JDBC.CommonSelect.Customers;
import JDBC.DBConnectPool.Druid.DruidConnect;

public class QueryRunnerTest {
	private static Connection conn;
	private static QueryRunner queryRunner;
	static {
		try {
			conn = DruidConnect.getConn();
			queryRunner = new QueryRunner();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	// 增删改
	public static void test01() throws Exception {
		String sql = "insert into customers (name,email,birth) values (?,?,?)";
		queryRunner.update(conn, sql, "test", "test@163.com", "1970-07-07");
	}

	// 查询(单一结果,包装成一个类)
	public static void test02() throws SQLException {
		String sql = "select name,email,birth from customers where id = ?";
		ResultSetHandler<Customers> rsh = new BeanHandler<Customers>(Customers.class);
		Customers customers = queryRunner.query(conn, sql, rsh, 1);
		System.out.println(customers);
	}

	// 查询(多条数据包装成一个BeanList)
	public static void test03() throws SQLException {
		String sql = "select name,email,birth from customers where id > ?";
		BeanListHandler<Customers> rsh = new BeanListHandler<Customers>(Customers.class);
		List<Customers> customerslist = queryRunner.query(conn, sql, rsh, 1);
		customerslist.forEach(System.out::println);
	}

	// 查询(单条数据包装成Map)
	public static void test04() throws SQLException {
		String sql = "select name,email,birth from customers where id = ?";
		MapHandler rsh = new MapHandler();
		Map map = queryRunner.query(conn, sql, rsh, 1);
		System.out.println(map);
	}

	// 查询(多条数据包装成MapList)
	public static void test05() throws SQLException {
		String sql = "select name,email,birth from customers where id > ?";
		MapListHandler rsh = new MapListHandler();
		List<Map<String, Object>> query = queryRunner.query(conn, sql, rsh, 1);
		query.forEach(System.out::println);
	}

	// 查询特殊值
	public static void test06() throws SQLException {
		String sql = "select count(*) from customers";
		ScalarHandler rsh = new ScalarHandler();
		Long count = (Long) queryRunner.query(conn, sql, rsh);
		System.out.println(count);
	}
}
ndler rsh = new ScalarHandler();
		Long count = (Long) queryRunner.query(conn, sql, rsh);
		System.out.println(count);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值