1. ResultSetHandler的几个实现类
public class TestResultSetHandler {
@Test
public void test1() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql = "select * from user where id=?";
Object[] arr = qr.query(sql, new ArrayHandler(), 1);
for (Object o : arr) {
System.out.println(o);
}
}
@Test
public void test2() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql = "select * from user";
List<Object[]> query = qr.query(sql, new ArrayListHandler());
for (Object[] objects : query) {
for (Object object : objects) {
System.out.print(object + " ");
}
System.out.println();
}
}
@Test
public void test3() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select username,password from user";
List<Object> list = qr.query(sql, new ColumnListHandler<Object>(2));
for(Object object:list){
System.out.println(object);
}
}
@Test
public void test4() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select * from user";
Map<Object, Map<String, Object>> query = qr.query(sql, new KeyedHandler<>(1),1);
Set<Object> set = query.keySet();
for(Object key:set){
System.out.println(key);
Map<String, Object> value = query.get(key);
Set<String> keySet = value.keySet();
for(String keyset:keySet){
System.out.println(keyset+"---"+value.get(keyset));
}
System.out.println();
}
}
@Test
public void test5() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select * from user where id=?";
Map<String, Object> query = qr.query(sql, new MapHandler(),2);
for(Map.Entry<String, Object> m:query.entrySet()){
System.out.println(m.getKey()+"------"+m.getValue());
}
}
@Test
public void test6() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select * from user";
List<Map<String, Object>> query = qr.query(sql, new MapListHandler());
for(Map<String, Object> list:query){
for(Map.Entry<String, Object> map:list.entrySet()){
System.out.println(map.getKey()+"----"+map.getValue());
}
System.out.println();
}
}
@Test
public void test7() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select username from user";
Object query = qr.query(sql, new ScalarHandler<Object>());
System.out.println(query);
}
@Test
public void test8() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select * from user where id=?";
User query = qr.query(sql, new BeanHandler<User>(User.class),2);
System.out.println(query);
}
@Test
public void test9() throws SQLException {
QueryRunner qr = new QueryRunner(C3P0Util.getDataSource());
String sql="select * from user where id=?";
List<User> query = qr.query(sql, new BeanListHandler<User>(User.class),2);
for(User user:query){
System.out.println(user);
}
}
}