Have talked about the use of QueryRunner insert, modify, update data, and now to learn about the use of QueryRunner database table query.
By query QueryRunner class () method to complete the query of the database table, but when the query need to implement the interface to the result set ResultSetHandler encapsulated into objects. You can achieve through their own interface, but it is clear that we should use the implementation class DBUtils provided by the toolkit to implement the package.
In DBUtils frame, a total of nine ResultSetHandler implementation class.
- ArrayHandler: the first row of the result set of data objects is converted into an array.
- ArrayListHandler: each row of the result set of data are transformed into an array of objects, and then stored in the List.
- BeanHandler: encapsulating the first data row to the result set corresponding to a JavaBean instance.
- BeanListHandler: Each row of data in the result set corresponding to a package to the JavaBean instance, stored in the List.
- ColumnListHandler: The result set data stored in a column of the List.
- MapHandler: The results of the first row in the package of data to a the Map, key column name, value is the corresponding value.
- MapListHandler: Each row of data in the result set into a package the Map, and then stored in List
- KeyedHandler (name): Each row of data in the result set into a package the Map (List
- ScalarHandler: The result set is converted into an information sequence object
Respectively, through case feel.
New Test class ResultSetHandlerTest
then add member variables
private ComboPooledDataSource dataSource = new ComboPooledDataSource();
Add ArrayHandler test code
@Test
public void testArrayHandler() throws SQLException{
//ArrayHandler 将结果集的第一行数据存入Object数组
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
//数组的每一个元素对应第一行数据的每一列
Object[] objects = queryRunner.query(sql, new ArrayHandler());
System.out.println(Arrays.toString(objects));
}
Run the code
to add ArrayListHandler test code
@Test
public void testArrayListHandler() throws SQLException{
//ArrayListHandler 将结果集的每一行数据存入Object数组,然后存入List
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
List<Object[]> list = queryRunner.query(sql, new ArrayListHandler());
for(Object[] objects : list){
System.out.println(Arrays.toString(objects));
}
}
Run the code
to add BeanHandler test code
@Test
public void testBeanHandler() throws SQLException{
//BeanHandler 将结果集的第一行数据封装到JavaBean对象中
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
//传入Account.class参数是为了在方法中通过反射构造Account对象实例
Account account = queryRunner.query(sql, new BeanHandler<Account>(Account.class));
System.out.println(account.getId());
System.out.println(account.getName());
System.out.println(account.getMoney());
}
Run the code
Note: Use BeanHandler, table column names must match the name attribute Bean class.
Add BeanListHandler test code
@Test
public void testBeanListHandler() throws SQLException{
//BeanListHandler 将结果集每一条数据都封装到JavaBean对象,再存入List
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
List<Account> list = queryRunner.query(sql, new BeanListHandler<Account>(Account.class));
for(Account account : list){
System.out.print(account.getId() + "\t");
System.out.print(account.getName() + "\t");
System.out.print(account.getMoney());
System.out.println();
}
}
Run the code
to add ColumnListHandler test code
@Test
public void testColumnListHandler() throws SQLException{
//ColumnListHandler 获得结果集的某一列
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
//泛型为什么写Object 因为每列的类型都不一样
List<Object> list = queryRunner.query(sql, new ColumnListHandler("name"));
System.out.println(list);
}
Run the code
to add MapHandler test code
@Test
public void testMapHandler() throws SQLException{
//MapHandler 将结果集中的第一行数据封装到Map集合,key是列名,value是数据值
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
Map<String, Object> map = queryRunner.query(sql, new MapHandler());
System.out.println(map);
}
Run the code
to add MapListHandler test code
@Test
public void testMapListHandler() throws SQLException {
// MapHandler 将结果集中的每一行数据封装到Map集合,key是列名,value是数据值,再将Map对象存入List
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
List<Map<String,Object>> list = queryRunner.query(sql, new MapListHandler());
for(Map<String,Object> map : list){
System.out.println(map);
}
}
Run the code
to add KeyedHandler test code
@Test
public void testKeyedHandler() throws SQLException {
// KeyedHandler 将结果集中的每一行数据都封装到Map里,再将Map存入一个Map里,key可以指定为任意列
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select * from account";
Map<Object, Map<String,Object>> map = queryRunner.query(sql, new KeyedHandler("name"));
System.out.println(map);
}
Run the code
to add ScalarHandler test code
@Test
public void testScalarHandler() throws SQLException{
//ScalarHandler 通常保存只有一行一列的结果数据
QueryRunner queryRunner = new QueryRunner(dataSource);
String sql = "select count(*) from account";
long count = (Long) queryRunner.query(sql, new ScalarHandler(1));
System.out.println(count);
}
Run code
here, it introduced nine Hanlder finished.
The most commonly used several:
BeanHandler, BeanListHandler, ColumnListHandler, ScalarHandler.