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); }}}