JDBC(12)—DBUtils工具类
来源:互联网 发布:mac双系统默认windows 编辑:程序博客网 时间:2024/06/08 19:12
DBUtils:commons-dbutils是Apache组织提供的一个开源JDBC工具库,它是对JDBC的简单封装,并且使用dbutils会极大的简化jdbc编码的工作量,同时不会影响到程序的性能。
两个重要组成:
类:QueryRunner:SQL语句的操作对象,可以设置查询结果集的封装策略,线程安全。
接口:ResultSetHandle:封装数据的策略对象——将封装结果集中的数据,转换到另一个对象
方法之间的关系
实例代码:
public class DBUtils_12 { //1.创建QueryRunner的实现类 QueryRunner queryRunner = new QueryRunner(); /** * 1.测试QueryRunner的实现类的update方法 * SQL语句可以是增、删、改 * 步骤: ①获取连接 * ②准备SQL语句 * ③QueryRunner对象调用update方法,同时把连接、SQL语句、占位符传进去 * ④关闭连接 */ @Test public void testUpdate(){ String sql = "delete from customers where id in(?,?)"; Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); //2.使用update方法 queryRunner.update(conn, sql, 19,20); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } } /** * 2.测试是QueryRunner的Query方法 * SQL语句是查询 * 重点:QueryRunner的query方法的返回值取决于其ResultSetHandler参数的handle方法的返回值 * 步骤: ①获取连接 * ②准备SQL语句 * ③QueryRunner对象调用query方法,同时传入连接、SQL语句、和 */ @Test public void testQuery(){ Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); String sql = "select id, name, age, birth, address from customers"; Object obj = queryRunner.query(conn, sql, new MyResultSetHandler()); System.out.println(obj); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } } //内部类 class MyResultSetHandler implements ResultSetHandler{ @Override public Object handle(ResultSet resultSet) throws SQLException{ /* System.out.println("handle..."); return "test";*/ List<Customers> customers = new ArrayList<>(); while(resultSet.next()){ Integer id = resultSet.getInt(1); String name = resultSet.getString(2); String age = resultSet.getString(3); Date birth = resultSet.getDate(4); String address = resultSet.getString(5); Customers customer = new Customers(id, name, age, birth, address); customers.add(customer); } return customers; } } /** * 一些比较重要的Handler方法 * BeanHandler() * BeanListHandler() * MapHandler() * MapListHandler() * ScalarHandler() * */ //BeanHandler:把结果集的第一条记录转为创建BeanHandler对象时传入的Class参数对应的对象。 @Test public void testBeanHandler(){ Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); String sql = "select id, name, age, birth,address from customers where id = ?"; Customers customer = queryRunner.query(conn, sql, new BeanHandler(Customers.class), 17); System.out.println(customer); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } } //BeanListHandler:把结果集转化为一个List,让list不为null,但有可能为空集合(size(),返回0) //若SQL语句能够查询到记录,List中存放创建BeanListHandler时传入的Class对象对应的对象 @Test public void testBeanListHandler(){ Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); String sql = "select id, name, age, birth,address from customers"; List<Customers> customers = queryRunner.query(conn, sql, new BeanListHandler(Customers.class)); System.out.println(customers); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } } //MapHandler:返回SQL语句对应的第一条记录对应的Map对象,键:SQL查询的列名(非别名) 值:列的值 @Test public void testMapHandler(){ Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); String sql = "select id, name, age, birth,address from customers"; Map<String, Object> result = queryRunner.query(conn, sql, new MapHandler()); System.out.println(result); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } } //MapListHandler:将结果集转为Map的List,Map对应查询的一条记录,MapListHandler返回多条记录对应的Map的集合 @Test public void testMapListHandler(){ Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); String sql = "select id, name, age, birth,address from customers"; List<Map<String, Object>> results = queryRunner.query(conn, sql, new MapListHandler()); System.out.println(results); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } } //ScalarHandler:把结果集转为一个数值返回,这个数值可以是任意数据类型的和字符串,Data等。 @Test public void testScalarHandler(){ Connection conn = null; try { conn = TestTools.getConnectionWithC3P0(); String sql = "select count(id) from customers"; Object results = queryRunner.query(conn, sql, new ScalarHandler()); System.out.println(results); } catch (Exception e) { e.printStackTrace(); }finally{ TestTools.release(null, conn); } }}
阅读全文
0 0
- JDBC(12)—DBUtils工具类
- JDBC-DBUtils工具类
- JDBC : 使用DBUtils 工具类
- JDBC相关(三):DButils工具类的使用详解
- 开源JDBC工具类之DbUtils解析
- JDBC笔记(六)DBUtils工具
- JDBC之DbUtils工具
- JDBC工具-DBUtils
- QueryRunner(DBUtils工具类)体现了:DBUtils封装了C3P0和jdbc。【三者的关系】
- (五)JDBC连接池&DBUtils—DBUtils完成CRUD
- JDBC操作工具类[来源于开源DbUtils]
- JDBC之数据库连接的java配置工具类dbUtils
- J2EE进阶之JDBC工具类DbUtils使用 二十
- Jdbc基础 工具类 连接池到dbutils
- DbUtils component学习使用(jdbc封装工具)
- JdbcUtils工具类(c3p0+DbUtils优化)
- 测试 DBUtils 工具类
- DBUtils工具类
- RxJava的使用(一)
- 代码规范
- FTP的dos命令使用方法
- 抽象类 abstract
- Hdu6058 Kanade's sum(2017多校第3场)
- JDBC(12)—DBUtils工具类
- 双线性插值法公式推导
- 白话经典算法系列之一 冒泡排序的三种实现
- xcode-select: error: tool 'xcodebuild' requires Xcode错误解决方法
- Tomcat 404 error: The origin server did not find a current representation for the target resource or
- 安装第四个虚拟机时出现Determining IP information for eth0...failed
- 百练1258:Agri-Net题解
- laravel 之 数据库
- 彻底理解 JS 中 this 的指向