In the previous article "use DbUtils to add, delete, modify, and query", we found that when executing the runner. query () line of code, we need to process the query result set by ourselves, which is troublesome. This line of code is prototype: publicObjectquery (Connectionconn, Stringsql, ResultSetHandlerTrsh, Object... params) Where ResultSet
In the previous article "use DbUtils to add, delete, modify, and query", we found that when executing the runner. query () line of code, we need to process the query result set by ourselves, which is troublesome. The prototype of this line of code is: public Object query (Connection conn, String SQL, ResultSetHandlerT rsh, Object... params) Where ResultSet
In the previous article "use DbUtils to add, delete, modify, and query", we found that when executing the runner. query () line of code, we need to process the query result set by ourselves, which is troublesome. The prototype of this line of code is:
public Object query(Connection conn, String sql, ResultSetHandler
rsh, Object... params)
Among them, ResultSetHandler is an interface. In fact, the omnipotent Apache has provided many easy-to-use implementation classes for us. The following is an example:
Public class RSHanlderDemo {// ScalarHandler: gets the value of the specified column in the first row of data in the result set. It is often used for single-value query @ Testpublic void tes9 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Long count = (Long) runner. query ("select count (*) from account", new ScalarHandler (); System. out. println (count);} // KeyedHandler (name): encapsulate each row of data in the result set in a Map (List), And then store these maps in a map. The key is the specified column. @ Testpublic void tes8 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Map
> Map = runner. query ("select * from account where money>? ", New KeyedHandler (" id "), 500); System. out. println (map);} // ColumnListHandler: stores the data of a column in the result set in the List. @ Testpublic void tes7 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); List
List = runner. query ("select * from account where money>? ", New ColumnListHandler (3), 500); System. out. println (list);} // MapListHandler: encapsulate each row of data in the result set in a Map, and store the data in List @ Testpublic void tes6 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); List
> List = runner. query ("select * from account where money>? ", New MapListHandler (), 500); System. out. println (list);} // MapHandler: encapsulate the first row of data in the result set into a Map. The key is the column name and the value is the corresponding value. @ Testpublic void tes5 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Map
Map = runner. query ("select * from account where money>? ", New MapHandler (), 500); System. out. println (map);} // BeanListHandler: encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in the List. @ Testpublic void tes4 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Listlist = runner. query ("select * from account where money>? ", New BeanListHandler (Account. class), 500); System. out. println (list);} // BeanHandler: encapsulate the first row of data in the result set into a corresponding JavaBean instance. @ Testpublic void tes3 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Account acc = runner. query ("select * from account where money>? ", New BeanHandler (Account. class), 500); System. out. println (acc);} // ArrayListHandler: converts each row of data in the result set into an array of objects and stores them in the List. @ Testpublic void tes2 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); List
List = runner. query ("select * from account where money>? ", New ArrayListHandler (), 500); System. out. println (list);} // ArrayHandler: converts the first row of data in the result set to an array of objects. @ Testpublic void test1 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Object [] objs = runner. query ("select * from account where money>? ", New ArrayHandler (), 500); System. out. println (objs );}}During testing, you can add a breakpoint for debugging and then execute Debug as JUnit Test.
Summary:
① ArrayHandler: converts the first row of data in the result set into an array of objects.
② ArrayListHandler: converts each row of data in the result set into an object array and stores it in the List.
③BeanHandler: Encapsulate the first row of data in the result set into a corresponding JavaBean instance.
④BeanListHandler: Encapsulate each row of data in the result set into a corresponding JavaBean instance and store it in the List.
⑤ MapHandler: encapsulate the first row of data in the result set into a Map. The key is the column name and the value is the corresponding value.
⑥ MapListHandler: encapsulate each row of data in the result set into a Map and store it in List.
7. ColumnListHandler: stores the data of a column in the result set to the List.
Define KeyedHandler (name): encapsulate each row of data in the result set into a Map (List), And then store these maps in a map. The key is the specified column.
⑨ ScalarHandler: obtains the value of the specified column in the first row of data in the result set,It is often used for single-value query.