ResultSetHandler结果集处理类(8种)
1,ArrayHandler:将结果集的第一行存储到对象分段中Object []
2,ArrayListHandler:将结果集合的每一行,封装到对象数组中,出现很多对象数组,然后封装到ListCollection中
3,BeanHandler:将结果集的第一行数据,封装成JavaBean对象
4,BeanListHandler:将数据结果集的每一行数据,封装成JavaBean对象,多个JavaBean对象封装到List集合中
5,ColumnListHandler:指定列的数据,存储到列表集合
6,ScalarHandler:对于查询结果,只有一个结果集。用泛型进行封装
7,MapHandler:将结果集第一行数据,放在地图中,Map <列名,这列的数据>
8,MapListHandler:将结果集每一行存储到map中,再把map集合存储到List集合中
八种方法代码演示:
package note.basics.dbutils;
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.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import note.basics.dbutils.domain.Sort;
import note.basics.dbutils.jdbcutil.JDBCUtilsConfig;
/*
* QueryRunner数据查询操作
* 调用QueryRunner类方法query(Connection con,String sql,ResultSetHandler r,Object ...params)
* ResultSetHandler r 结果集的处理方式,传递ResulSetHandler接口实现类
* Object ...params SQL语句中的?占位符
*
* 注意:query方法返回值,返回的是T 泛型,具体返回值类型,跟随结果集处理方式变化
*/
public class QueryRunnerDemo {
private static Connection con = JDBCUtilsConfig.getConnection();
public static void main(String[] args) throws SQLException {
mapListHandler();
}
/*
* 结果集的第一种处理方法,ArrayHandler
* 将结果集的第一行存储到对象数组中 Object[]
*/
public static void arrayHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用方法query执行查询,传递连接对象,SQL语句,结果集处理方式的实现类
//返回对象数组
Object[] result = qr.query(con, sql,new ArrayHandler());
for(Object obj : result){
System.out.println(obj);
}
}
/*
* 结果集第二张处理方法,ArrayListHandler
* 将结果集的每一行,封装到对象数组中,出现很多对象数组
* 对象数组存储到List集合
*/
public static void arrayListHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用query方法,结果集处理的参数上,传递实现类ArrayListHandler
//方法返回值 每一行的对象数组,存储到List
List<Object[]> result = qr.query(con,sql,new ArrayListHandler());
//集合的遍历
for (Object[] objects : result) {
//便利对象数组
for (Object object : objects) {
System.out.println(object);
}
}
}
/*
* 结果集第三种处理方法,BeanHandler
* 将结果集的第一行数据,封装成JavaBean对象
*/
public static void beanHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//调用方法,传递结果集实现类BeanHandler
Sort s = qr.query(con, sql,new BeanHandler<Sort>(Sort.class));
System.out.println(s);
}
/*
* 结果集第四种处理方法,BeanListHandler
* 将数据结果集的每一行数据,封装成JavaBean对象
* 多个JavaBean对象封装到List集合中
*/
public static void beanListHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
List<Sort> query = qr.query(con, sql,new BeanListHandler<Sort>(Sort.class));
for (Sort sort : query) {
System.out.println(sort);
}
}
/*
* 结果集第五种方法,ColumnListHandler
* 结果集,指定列的数据,存储到List集合
*/
public static void columnListHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
//可以写列的编号,也可以写列名 建议写列名
List<Object> query = qr.query(con, sql, new ColumnListHandler<Object>("pprice"));
for (Object object : query) {
System.out.println(object);
}
}
/*
* 结果集第六种处理方法,ScalarHandler
* 对于查询结果,只有一个结果集
*/
public static void scalarHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT COUNT(*) FROM sort";
Object query = qr.query(con,sql,new ScalarHandler<Object>());
System.out.println(query);
}
/*
* 结果集第七种处理方法,MapHandler
* 将结果集第一行数据,放在map中
* Map<列名,这列的数据>
*/
public static void mapHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
Map<String, Object> query = qr.query(con, sql,new MapHandler());
for(String key : query.keySet()){
System.out.println(key +" "+query.get(key));
}
}
/*
* 结果集第八种处理方法,MapListHandler
* 将结果集每一行存储到map中 键:列名 值:数据
* 再把map集合存储到List集合中
*/
public static void mapListHandler() throws SQLException{
QueryRunner qr = new QueryRunner();
String sql = "SELECT * FROM sort";
List<Map<String, Object>> query = qr.query(con, sql,new MapListHandler());
for (Map<String, Object> map : query) {
for(String key : map.keySet()){
System.out.print(map.get(key) + "\t");
}
System.out.println();
}
}
}
用到的JavaBean
package note.basics.dbutils.domain;
public class Sort {
private int id;
private String pname;
private int pprice;
private String pdesc;
public void Sort(int id,String pname,int pprice,String pdesc){
this.id = id;
this.pname = pname;
this.pprice = pprice;
this.pdesc = pdesc;
}
public void Sort(){
}
@Override
public String toString() {
return "Sort [id=" + id + ", pname=" + pname + ", pprice=" + pprice + ", pdesc=" + pdesc + "]";
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPname() {
return pname;
}
public void setPname(String pname) {
this.pname = pname;
}
public int getPprice() {
return pprice;
}
public void setPprice(int pprice) {
this.pprice = pprice;
}
public String getPdesc() {
return pdesc;
}
public void setPdesc(String pdesc) {
this.pdesc = pdesc;
}
}
用到的连接数据库工具类
package note.basics.dbutils.jdbcutil;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.Properties;
/*
* 编写数据库连接的工具类,JDBC工具类
* 获取连接对象采用读取配置文件方式
* 读取文件获取连接,执行一次,static{}
*/
public class JDBCUtilsConfig {
private static Connection con ;
private static String driverClass;
private static String url;
private static String username;
private static String password;
static{
try{
readConfig();
Class.forName(driverClass);
con = DriverManager.getConnection(url, username, password);
}catch(Exception ex){
throw new RuntimeException("数据库连接失败");
}
}
private static void readConfig()throws Exception{
InputStream in = JDBCUtilsConfig.class.getClassLoader().getResourceAsStream("database.properties");
Properties pro = new Properties();
pro.load(in);
driverClass=pro.getProperty("driverClass");
url = pro.getProperty("url");
username = pro.getProperty("username");
password = pro.getProperty("password");
}
public static Connection getConnection(){
return con;
}
}