码迷,mamicode.com
首页 > 其他好文 > 详细

QueryRunner类结果集处理的八种方法

时间:2020-11-11 16:51:56      阅读:25      评论:0      收藏:0      [点我收藏+]

标签:不同   调用   scalar   存储   cal   数据   handler   lan   rom   

在连接数据库的时候(MySQL)时,会遇到的问题。

调用QueryRunner的方法是:

query(Connection con,String Sql,ResultSetHandler r,Object.params)

MapListHandler

将结果集每一行存储到Map集合中,键:列名;值:数据

public static void mapListHandler()throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql = "select * from db";
    Lsit<Map<String,Object>> list = qr.query(con,sql,new MapListHandler());
    for(Map<String,Object> map:list){
        System.out.print(key+‘..‘+map.get(key));
    }
    System.out.prinln();
}

MapHandler

将结果集的第一行数据,封存到Map集合中

public static void mapHandler()throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql = "select * from db";
    Map map = qr.query(con,sql,new MapHandler());
    for(String key:map.keySet()){
        System.out.print(key+‘..‘+map.get(key));
    }
    System.out.prinln();
}

ScalarHandler

查询后,只有一个结果

Public static void scalarHandler() throws SQLException(){
    QueryRunner qr = new QueryRunner();
    String sql = "select * from db";
    long count = qr.query(con,sql,new ScalarHandler());
    System.out.prinln(count);
}

ColumnListHandler

结果集,指定列的数据存储到List集合中,

List每个列数据类型不同。

public static void columnListHandler()throws SQLException(){
    QueryRunner qr = new QueryRunner();
    String sql = "select * from db";
    List<Object> list = qr.query(con,sql,new ColumnListHandler<Object>("name"));
    for(Object obj:list){
        System.out.println(obj);
    }
    System.out.prinln(obj);
}

BeanListHandler

结果集每一行数据,封装JavaBean对象

多个JavaBean对象,存储到List集合

public static void columnListHandler()throws SQLException(){
    QueryRunner qr = new QueryRunner();
    String sql = "select * from db";
    List list = qr.query(con,sql,new BeanListHandler<Sort>(Sort.class));
    for(Sort s:list){
        System.out.println(s);
    }
}

BeanHandler

将结果集的第一行数据,封装成JavaBean对象

public static void beanHandler()throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql = "SELECT * FROM sort ";
    Sort s = qr.query(con, sql, new BeanHandler<Sort>(Sort.class));
    System.out.println(s);
}

ArrayListHandler

将结果集的每一行,封装到对象数组中

public static void arrayListHandler()throws SQLException{
	QueryRunner qr = new QueryRunner();
	String sql = "SELECT * FROM sort";
    List<Object[]> result=  qr.query(con, sql, new ArrayListHandler());
    for( Object[] objs  : result){
        for(Object obj : objs){
            System.out.print(obj+"  ");
        }
        System.out.println();
    }
}

ArrayHandler

将结果集的第一行存储到对象数组中 Object[]

public static void arrayHandler()throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql = "SELECT * FROM sort";
    Object[] result = qr.query(con, sql, new ArrayHandler());
    for(Object obj : result){
    	System.out.print(obj);
    	}
    }
}

QueryRunner类结果集处理的八种方法

标签:不同   调用   scalar   存储   cal   数据   handler   lan   rom   

原文地址:https://www.cnblogs.com/Believe-oneself/p/13768476.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!