JAVA-使用DbUtils獲得各種結果集物件
package com.dxm.demo3;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
import java.util.Set;
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 com.dxm.domain.Sort;
import com.dxm.jdbcutil.JDBCUtilsConfig;
import com.sun.org.apache.bcel.internal.generic.NEW;
public class QueryRunnerDemo2 {
static Connection con = JDBCUtilsConfig.getConnection();
public static void main(String[] args) {
try {
//arrayHandler();
//arrayListHandler();
//beanHandler();
//beanListHandler();
//columnListHandler();
//scalarHandler();
//mapHandler();
mapListHandler();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private static void arrayHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
Object[]result = qRunner.query(con, sql, new ArrayHandler());
for (Object object : result) {
System.out.println(object);
}
}
private static void arrayListHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
List<Object[]>result = qRunner.query(con, sql, new ArrayListHandler());
for (Object[] objects : result) {
for (Object object : objects) {
System.out.print(object+"\t");
}
System.out.println();
}
}
private static void beanHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
Sort sort= qRunner.query(con, sql, new BeanHandler(Sort.class));
System.out.println(sort);
}
private static void beanListHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
List<Sort> lst=qRunner.query(con, sql, new BeanListHandler(Sort.class));
for (Sort sort : lst) {
System.out.println(sort);
}
}
private static void columnListHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
List<Object> lst=qRunner.query(con, sql, new ColumnListHandler<>("sname"));
for (Object object : lst) {
System.out.println(object);
}
}
private static void scalarHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select count(*) from sort";
long count=qRunner.query(con, sql, new ScalarHandler<Long>());
System.out.println(count);
}
private static void mapHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
Map<String, Object> map=qRunner.query(con, sql, new MapHandler());
for (String key : map.keySet()) {
System.out.println(key +"\t"+map.get(key));
}
}
private static void mapListHandler() throws SQLException {
QueryRunner qRunner=new QueryRunner();
String sql="select * from sort";
List<Map<String, Object>> list = qRunner.query(con, sql, new MapListHandler());
for (Map<String, Object> map : list) {
for (String key : map.keySet()) {
System.out.print(key+"==="+map.get(key)+"\t");
}
System.out.println();
}
}
}