private QueryRunner qr= new QueryRunner(DbcpUtil.getDataSource());

package com.tfy.itheima.impl.test;


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


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.ColumnListHandler;
import org.apache.commons.dbutils.handlers.KeyedHandler;
import org.apache.commons.dbutils.handlers.MapHandler;
import org.apache.commons.dbutils.handlers.MapListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import org.junit.Test;


import com.tfy.itheima.jdbc.util.DbcpUtil;




public class ResultSetHandleTest {
private QueryRunner qr= new QueryRunner(DbcpUtil.getDataSource());
@Test
//ArrayHandler:把结果集中的第一行数据转成对象数组。
public void test1(){
try {
String sql="select * from person";

Object []objs=qr.query(sql, new ArrayHandler());
for(Object o:objs){
System.out.println(o);
}
} catch (SQLException e) {
e.printStackTrace();
}


}
@Test
//ArrayListHandler:把结果集中的每一行数据都转成一个数组,再存放到List中。
public void test2(){
try{
String sql="select * from person";
List<Object[]> list=qr.query(sql, new ArrayListHandler());
for(Object[] objs:list){
System.out.println("-----------------------");
for(Object o:objs){
System.out.println(o);
}
}

}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}

}
@ Test
//ColumnListHandler:将结果集中某一列的数据存放到List中。
public void test3(){
try{
String sql="select * from person";
List<Object> list = qr.query(sql, new ColumnListHandler("name"));

for(Object o:list){
System.out.println(o);
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
//KeyedHandler:将结果集中的每一行数据都封装到一个Map<列名,列值>里,再把这些map再存到一个map里,其key为指定的key。
public void test4(){
try{
String sql="select * from person";
Map<Object, Map<String, Object>> bmap = qr.query(sql, new KeyedHandler());

for(Map.Entry<Object, Map<String , Object>> bme:bmap.entrySet() ){
Map<String, Object> lmap = bme.getValue();
System.out.println("------------------------");
for(Map.Entry<String,Object> lme:lmap.entrySet() ){
System.out.println(lme.getKey()+"="+lme.getValue());

}
}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
//MapHandler:将结果集中的第一行数据封装到一个Map里,key是列名,value就是对应的值
public void test5(){
try{
String sql="select * from person";
Map<String, Object> map = qr.query(sql, new MapHandler());
for(Map.Entry<String, Object> lme:map.entrySet()){
System.out.println(lme.getKey()+"="+lme.getValue());

}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
@Test
//MapListHandler:将结果集中的每一行数据都封装到一个Map里,然后再存放到List
public void test6(){
try{
String sql="select * from person";
List<Map<String, Object>> list = qr.query(sql, new MapListHandler());
for(Map<String, Object> map:list){
System.out.println("-------------------");
for(Map.Entry<String, Object> me:map.entrySet()){
System.out.println(me.getKey()+"="+me.getValue());
}

}
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException();
}
}
@Test
//ScalarHandler:用于只有一行且只有一列的情况。比如统计记录条数
public void test7(){
try{
String sql="select * from person";
Object obj = qr.query(sql, new ScalarHandler(1));
System.out.println(obj.getClass().getName());

int num=((Integer )obj).intValue();
// int num=((Long )obj).intValue();

num=(Integer)obj;
System.out.println(num);
}catch(Exception e){
e.printStackTrace();
throw new RuntimeException(e);
}
}
}

你可能感兴趣的:(dataSource)