博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
QueryRunner类的八种结果处理集
阅读量:5954 次
发布时间:2019-06-19

本文共 9604 字,大约阅读时间需要 32 分钟。

package cn.jy.demo;

 

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;

 

public class QueryRunnerdemo0 {

private  static Connection con=JDBCUtilsConfig.getConnection();

public static void main(String[] args) throws Exception {

//arrayHandler();

//arrayListHandler();

//beanHandler();

//beanListHandler();

//columnListHandler();

//scalarHandler();

//mapHandler();

mapListHandler();

}

public static void mapListHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

String sql="SELECT * FROM zhangwu1";

List<Map<String, Object>> query = qr.query( con,sql, new MapListHandler());

DbUtils.close(con);

for(Map<String,Object> map:query){

for(String key:map.keySet()){

System.out.print(key+"  "+map.get(key));

}

System.out.println();

}

}

public static void mapHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

String sql="SELECT * FROM zhangwu1";

Map<String, Object> query = qr.query(con,sql, new MapHandler());

DbUtils.close(con);

for(String key:query.keySet()){

System.out.println(key+"   "+query.get(key));

 

}

}

public static void scalarHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

String sql="SELECT COUNT(*) FROM zhangwu1";

Object query = qr.query(con,sql, new ScalarHandler<Object>());

System.out.println(query);

}

public static void columnListHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

String sql="SELECT*FROM zhangwu1";

List<Object> query = qr.query(con,sql,new ColumnListHandler<Object>("flname"));

DbUtils.close(con);

for(Object ob:query){

System.out.println(ob);

}

}

public static void beanListHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

String sql="SELECT*FROM zhangwu1";

List<zhangwu1> query = qr.query(con,sql, new BeanListHandler<>(zhangwu1.class));

DbUtils.close(con);

for(zhangwu1 ob:query){

System.out.println(ob);

}

}

public static void beanHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

String sql="SELECT*FROM zhangwu1";

zhangwu1 query = qr.query(con,sql, new BeanHandler<>(zhangwu1.class));

DbUtils.close(con);

System.out.println(query);

}

public static void arrayListHandler() throws SQLException{

 

QueryRunner qr = new QueryRunner();

String sql="SELECT*FROM zhangwu1";

List<Object[]> query = qr.query(con,sql, new ArrayListHandler());

DbUtils.close(con);

    for(Object[] obj:query){

    for(Object ob:obj){

    System.out.print(ob);

    }

    System.out.println();

    }

}

public static void arrayHandler() throws SQLException{

QueryRunner qr = new QueryRunner();

            String sql="SELECT * FROM zhangwu1";

       Object[] qu= qr.query(con,sql,new ArrayHandler());

       DbUtils.close(con);

       for(Object obj:qu){ 

       System.out.println(obj);

       }

             

 }

}

 

package cn.jy.demo;

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;
public class QueryRunnerdemo {private static Connection con =JDBCUtilsConfig.getConnection();public static void main(String[] args) throws SQLException  {//arrayHandler2();//arrayListHandler2();//beanHandler2();//beanListHandler2();//columnListHandler2();  // scalarHandler1();  数量 处理//scalarHandler();//mapHandler2();mapListHandler2();}//11.8结果集处理MapListHandlerpublic static void mapListHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";//调用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();}}public static void mapListHandler2() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";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结果集处理MapHandlerpublic static void mapHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";//调用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));}}public static void mapHandler2() throws SQLException{ QueryRunner qr=new QueryRunner(); String sql="SELECT * FROM zhangwu1"; Map <String ,Object> map=qr.query(con,sql, new MapHandler()); for(String key:map.keySet()){ System.out.println(key+"  "+map.get(key)); }}//11.6结果集处理ScalarHandler     查询后只有一个结果public static void scalarHandler1() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT COUNT(*) FROM zhangwu1";//调用QueryRunner方法   执行查询   返回列名的集合 Long ls = qr.query(con, sql, new ScalarHandler<Long>());DbUtils.close(con);System.out.println(ls);}public static void scalarHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT COUNT(*)FROM zhangwu1";Object query = qr.query(con,sql, new ScalarHandler<Object>());System.out.println(query);}//11.5结果集处理ColumnListHandlerpublic static void columnListHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";//调用QueryRunner方法   执行查询   返回列名的集合 List<Object> ls = qr.query(con, sql, new ColumnListHandler<Object>("flname"));DbUtils.close(con);for(Object l:ls){System.out.println(l);}}public static void columnListHandler2() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT*FROM zhangwu1";List<Object> ls=qr.query(con,sql, new ColumnListHandler<Object>("flname"));DbUtils.close(con);for(Object obj: ls){System.out.println(obj);}}//11.4结果集处理BeanListHandlerpublic static void beanListHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";//调用QueryRunner方法   执行查询   返回BeanListHandler List<zhangwu1> ls = qr.query(con, sql, new BeanListHandler<zhangwu1>(zhangwu1.class));DbUtils.close(con);for(zhangwu1 l:ls){System.out.println(l);}}public static void beanListHandler2() throws SQLException{QueryRunner qr = new QueryRunner();    String sql="SELECT * FROM zhangwu1";    List<zhangwu1> query = qr.query(con, sql,new BeanListHandler<zhangwu1>(zhangwu1.class));    for(zhangwu1 qiang:query){   System.out.println(qiang);    }}//11.3结果集处理BeanHandlerpublic static void beanHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";//调用QueryRunner方法   执行查询   返回BeanHandler zhangwu1 s = qr.query(con, sql, new BeanHandler<>(zhangwu1.class));DbUtils.close(con);System.out.println(s);}public static void beanHandler2() throws SQLException{QueryRunner qr=new QueryRunner();String sql="SELECT * FROM zhangwu1";zhangwu1 query = qr.query(con,sql,new BeanHandler<>(zhangwu1.class));DbUtils.close(con);     System.out.println(query);}//11.2结果集处理ArrayListHandler   打印表格中的所有的数据public static void arrayListHandler() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT  *         FROM zhangwu1";//调用QueryRunner方法   执行查询   返回的是集合// List<Object[]> query = qr.query(con, sql, new ArrayListHandler());//DbUtils.close(con);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();}//for(Object[] qiang:query){//for(Object obj:qiang){//System.out.print(obj+"    ");//}//System.out.println();//}}public static void arrayListHandler2() throws SQLException{QueryRunner qr=new QueryRunner();String sql="SELECT*FROM zhangwu1";List<Object[]> query = qr.query(con,sql, new ArrayListHandler());DbUtils.close(con);    for(Object[]  qiang:query){   for(Object s: qiang){   System.out.print(s);   }   System.out.println();    }}//1结果集处理ArrayHandler 打印表格中的第一行数据public static void arrayHandler1() throws SQLException{QueryRunner qr = new QueryRunner();String sql="SELECT * FROM zhangwu1";//调用QueryRunner方法   执行查询Object[] query = qr.query(con, sql, new ArrayHandler());DbUtils.close(con);for(Object qiang:query){System.out.println(qiang);}}public static void arrayHandler2() throws SQLException{QueryRunner qr = new QueryRunner();     String sql="SELECT * FROM zhangwu1";     Object[] query = qr.query(con, sql, new ArrayHandler());     DbUtils.close(con);     for(Object obj :query){    System.out.println(obj);     }}public static void arrayHandler() throws SQLException{QueryRunner qr = new QueryRunner();         String sql="SELECT * FROM zhangwu1";         Object[] query=qr.query(con,sql,new ArrayHandler());         DbUtils.close(con);         for(Object qiang:query){         System.out.println(qiang);         }}}

 

转载于:https://www.cnblogs.com/Fisherman13/p/10437204.html

你可能感兴趣的文章
21天让你成为Horizon View高手—Day11:手动池的创建
查看>>
Python迭代对象、迭代器、生成器
查看>>
请求转发与重定向的区别
查看>>
大数据分析 | 百年奥运往事知多少
查看>>
矩形覆盖-----批了外皮的亲蛙跳
查看>>
@RequestParam今天才知道是咋用的..
查看>>
全国第一家FPGA云主机(FAAS)正式启动售卖,被阿里云抢先了。
查看>>
Linux 局域网路由新手指南:第 2 部分
查看>>
TensorSpace:超酷炫3D神经网络可视化框架
查看>>
横向ListView (二)—— 添加快速滚动功能及item相关事件实现
查看>>
java 开发银行支付、对账时证书相关的操作总结
查看>>
为什么你的缓存更新策略是先更新数据库后删除缓存,讲讲其他的情况有什么问题?...
查看>>
计数服务设计
查看>>
如何在windows中使用cmd命令去编译,运行C++程序
查看>>
solidity语言开发智能合约
查看>>
再有人问你Netty是什么,就把这篇文章发给他
查看>>
centos7安装kibana5.x
查看>>
svn常用命令
查看>>
OSChina 周五乱弹 —— 你用学习机来搞学习?
查看>>
利于前台开发的两大工具flex和vue
查看>>