ArrayHandler |
将结果集中的第一条记录封装到一个Object[]数组中,数组中的每一个元素就是这条记录中的每一个字段的值 |
ArrayListHandler |
将结果集中的每一条记录都封装到一个Object[]数组中,将这些数组在封装到List集合中。 |
BeanHandler |
将结果集中第一条记录封装到一个指定的javaBean中。 |
BeanListHandler |
将结果集中每一条记录封装到指定的javaBean中,将这些javaBean在封装到List集合中 |
ColumnListHandler |
将结果集中指定的列的字段值,封装到一个List集合中 |
ScalarHandler |
它是用于单数据。例如select count(*) from 表操作。 |
MapHandler |
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据 |
MapListHandler |
将结果集第一行封装到Map集合中,Key 列名, Value 该列数据,Map集合存储到List集合 |
- ArrayHandler:
public static void myArrayHandler() throws SQLException { Connection con = MyJDBCUtiles.getConnection(); String sql = "select * from userinfo where age = ?"; Object [] param = {21}; QueryRunner qr = new QueryRunner(); Object [] result = qr.query(con,sql,new ArrayHandler(),param); for(Object res : result){ System.out.print(res+" "); } DbUtils.closeQuietly(con); }
- 打印结果:
- ArrayListHandler(注意接收结果集的数据类型)
public static void myArrayListHandler() throws SQLException { Connection con = MyJDBCUtiles.getConnection(); String sql = "select * from userinfo where age = ?"; Object [] param = {21}; QueryRunner qr = new QueryRunner(); List<Object[]> list = qr.query(con,sql,new ArrayListHandler(),param); for(Object[] obj : list){ for (int i = 0; i < obj.length; i++) { System.out.print(obj[i]+" "); } System.out.println(); } DbUtils.closeQuietly(con); }
- 打印结果:
- BeanHandler:
- 存储信息的类需要满足以下条件
- 需要实现接口:java.io.Serializable ,通常实现接口这步骤省略了,不会影响程序;
-
提供私有字段:private 类型 字段名;
- 提供getter/setter方法;
-
提供无参构造。
存储信息的类(Userinfo.java):
package com.kong.domain; public class Userinfo { private int id; private String username; private String password; private int age; public Userinfo(int id, String username, String password, int age) { this.id = id; this.username = username; this.password = password; this.age = age; } public Userinfo(){} public int getId() { return id; } public void setId(int id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
- 实现代码:
//使用beanHandler public static void beanHandler() throws SQLException { Connection con = MyJDBCUtiles.getConnection(); QueryRunner qr = new QueryRunner(); String sql = "select * from userinfo where age = ?"; Object [] param = {21}; Userinfo userinfo = qr.query(con,sql,new BeanHandler<Userinfo>(Userinfo.class),param); //若没有查询到则会报空指针异常 System.out.println(userinfo.getId()+" "+userinfo.getUsername()+" "+ userinfo.getPassword()+" "+userinfo.getAge());
DbUtils.closeQuietly(con); }
- 结果
- 使用BeanListArray
public static void beanListHandler() throws SQLException { Connection con = MyJDBCUtiles.getConnection(); QueryRunner qr = new QueryRunner(); String sql = "select * from userinfo where age = ?"; Object [] param = {21}; List<Userinfo> list = qr.query(con,sql,new BeanListHandler<Userinfo>(Userinfo.class),param); //若没有查询到则list.size()=0 for(Userinfo userinfo : list){ System.out.println(userinfo.getId()+" "+userinfo.getUsername()+" "+ userinfo.getPassword()+" "+userinfo.getAge()); } DbUtils.closeQuietly(con); }
- 结果
^_^