QueryRunner类的八种结果处理集
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); }}}
QueryRunner类的八种结果处理集的更多相关文章
- 【转载】JavaWeb之DBUtils QueryRunner类对数据表的增、删、查(8种结果集处理方式)、改操作
一.使用QueryRunner类,实现对数据表的 insert delete update package com.shuhuadream.queryrunner; import java.sql.C ...
- Java常用类之【八种基本数据类型】
一.装箱和拆箱 装箱:将基本数据类型包装为对应的包装类对象 拆箱:将包装类对象转换成对应的基本数据类型 JDK5.0中为基本数据类型提供了自动装箱(boxing).拆箱(unboxing)功能 二.八 ...
- c++ 类的默认八种函数
c++ 类的默认八种函数 #define _CRT_SECURE_NO_WARNINGS #include <iostream> #include <string> #incl ...
- JAVA四类八种基本数据类型
boolean类型 Boolean在内存中占用一个字节. 当java编译器把java源代码编译为字节码时,会用int或byte来表示boolean.在java虚拟机中,用整数零来表示false,用任意 ...
- Java大数类BigDecimal及八种舍入模式的介绍
BigDecimal的引入 在利用Java编程语言开发银行.金融类等需要对数值进行高精度计算的软件时,我们经常使用BigDecimal和BigInteger这两个大数据类,而不是常见的int.long ...
- Java四类八种
四类: 整数类型,浮点类型,字符型,布尔型 八种: byte,short,int,long,float,double,char,boolean
- Day1---Java 基本数据类型 - 四类八种 --九五小庞
一.Java四大数据类型分类 1.整型 byte .short .int .long 2.浮点型 float . double 3.字符型 char 4.布尔型 boolean 二.八种基本数据类型 ...
- pache—DBUtils框架简介、DbUtils类、QueryRunner类 、ResultSetHandler接口
Apache—DBUtils框架简介.DbUtils类.QueryRunner类 .ResultSetHandler接口 commons-dbutils 是 Apache 组织提供的一个开源 JDBC ...
- QueryRunner类
该类简单化了SQL查询,它与ResultSetHandler组合在一起使用可以完成大部分的数据库操作,能够大大减少编码量. QueryRunner类提供了两个构造方法: 默认的构造方法 需要一个 ja ...
随机推荐
- excel 数据量较大边查询边输入到excel表格中
public Resultmodel getexpenseMessagx(HttpServletResponse response, String date1, String date2) { lon ...
- 【Django】ORM操作MySQL数据库遇到的一些问题
关于查询操作: 1.exact和iexact exact相当于= iexact相当于like(但是这里的like和数据库的不一样,没有给后面条件加上%%所以这里like和=的作用相似) artic ...
- ASP.NET 在请求中检测到包含潜在危险的数据,因为它可能包括 HTML 标记或脚本
<textarea><%=Server.HtmlEncode(strContent)%></textarea> 转载:https://www.cnblogs.com ...
- oracle 11g SQL语句补充学习
添加列: alter table tablename add columnName datatype (not null); -------需要注意一点的是在添加一列为非空的时候, 表必 ...
- 转化为分组背包 zoj 3769
题目链接:https://vjudge.net/problem/ZOJ-3769 题意:现在你要去打怪,你有13种装备,每件装备会有伤害和防御两种属性,一般来说,每种装备只可以装备一件,但是特别的,戒 ...
- Codeforces977D ---Divide by three, multiply by two 深搜+map存出现的数
传送门:点我 题意:给定n长度的序列,重排成后一个数是前一个数除以三,或者后一个数是前一个数乘二,要求输出这个序列. 思路:大力深搜,对每个数搜除3的和乘2的是否出现过,然后继续搜下去.如果有一个数搜 ...
- if (HttpContext.Current.User.Identity.IsAuthenticated) 权限验证总是true
将浏览器关闭重启. 注:该语句是判断用户是否经过验证.
- [转]Docker到底是什么?为什么它这么火?
如果你是数据中心或云计算IT圈子的人,这一年多来应该一直在听到普通的容器.尤其是Docker,关于它们的新闻从未间断过.Docker1.0在今年6月发布后,声势更是达到了前所未有的程度. 动静之所以这 ...
- vue初学:基础概念
一.vue使用步骤: 1.引包vue.js 2.html中写要操作的DOM节点 3.创建vue对象:new Vue({options}); 4.配置options:el:(要操作的对象,用选择器,同j ...
- 总结函数open与fopen的区别
转自:https://www.zybuluo.com/yiltoncent/note/87461 对于这两个名字很类似的函数,对于很多初学者来说,不容易搞清楚它们有什么不同,只知道按照函数用法使用.如 ...