• 结果集的工具类处理方式


    package cn.lijun.QueryRunnerDemo1;

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

    import org.apache.commons.dbutils.DbUtils;
    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 cn.liju.jdbcutils.JDBCUtilsConfig;
    import cn.lijun.domain.Sort;

    public class QueryRunnerdemo {
    private static Connection con = JDBCUtilsConfig.getConection();
    public static void main(String[] args) throws SQLException {
    //1结果集处理ArrayHandler
    //arrayHandler();
    //arrayListHandler();
    //beanHandler();
    //beanListHandler();
    //columnListHandler();
    // scalarHandle();
    //mapHandler();
    mapListHandler();
    }
    //11.8结果集处理MapListHandler
    public static void mapListHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询 返回列名的集合
    List<Map<String,Object>> list= qr.query(con, sql, new MapListHandler());
    DbUtils.close(con);
    //遍历集合
    for(Map<String,Object> map:list){
    for(String key:map.keySet()){
    System.out.print(key+":"+map.get(key)+" ");
    }
    System.out.println();
    }

    }

    //11.7结果集处理MapHandler
    public static void mapHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询 返回列名的集合
    Map<String,Object> map= qr.query(con, sql, new MapHandler());
    DbUtils.close(con);

    for(String key:map.keySet()){
    System.out.println(key+" "+map.get(key));
    }

    }
    //11.8结果集处理MapListHandler
    //11.2结果集处理ArrayListHandler
    //11.6结果集处理ScalarHandler 查询后只有一个结果
    public static void scalarHandle() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT COUNT(*) FROM sort";
    //调用QueryRunner方法 执行查询 返回列名的集合
    Long ls = qr.query(con, sql, new ScalarHandler<Long>());
    DbUtils.close(con);

    System.out.println(ls);

    }


    //11.5结果集处理ColumnListHandler
    public static void columnListHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询 返回列名的集合
    List<Object> ls = qr.query(con, sql, new ColumnListHandler<Object>("sname"));
    DbUtils.close(con);

    for(Object l:ls){
    System.out.println(l);
    }

    }


    //11.4结果集处理BeanListHandler
    public static void beanListHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询 返回BeanListHandler
    List<Sort> ls = qr.query(con, sql, new BeanListHandler<Sort>(Sort.class));
    DbUtils.close(con);

    for(Sort l:ls){
    System.out.println(l);
    }

    }

    //11.3结果集处理BeanHandler
    public static void beanHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询 返回BeanHandler
    Sort s = qr.query(con, sql, new BeanHandler<>(Sort.class));
    DbUtils.close(con);


    System.out.println(s);
    }

    //11.2结果集处理ArrayListHandler
    public static void arrayListHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询 返回的是集合
    List<Object[]> query = qr.query(con, sql, new ArrayListHandler());
    DbUtils.close(con);


    for(Object[] qiang:query){
    for(Object obj:qiang){
    System.out.print(obj+" ");
    }
    System.out.println();
    }
    }

    //1结果集处理ArrayHandler
    public static void arrayHandler() throws SQLException{
    QueryRunner qr = new QueryRunner();
    String sql="SELECT * FROM sort";
    //调用QueryRunner方法 执行查询
    Object[] query = qr.query(con, sql, new ArrayHandler());
    DbUtils.close(con);


    for(Object qiang:query){
    System.out.println(qiang);
    }
    }
    }

  • 相关阅读:
    The executable was signed with invalid entitlements
    iOS7 文本转语音 AVSpeechSynthesizer
    The document "ViewController.xib" could not be opened. Could not read archive.
    UIPanGestureRecognizer
    UINavigationController
    IE6下面的css调试工具
    15款最好的网站音乐播放器
    ASP.NET常用加密解密方法
    ASP.NET根据IP获取省市地址
    强悍的CSS工具组合:Blueprint, Sass, Compass
  • 原文地址:https://www.cnblogs.com/lijun6/p/10431919.html
Copyright © 2020-2023  润新知