Use DbUtils to add, delete, modify, and query the implementation class of the ResultSetHandler interface.

Source: Internet
Author: User

Use DbUtils to add, delete, modify, and query the implementation class of the ResultSetHandler interface.

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<T> 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 into a Map (List <Map> ), store these maps in a map, and the key is the specified column. @ Testpublic void tes8 () throws SQLException {QueryRunner runner = new QueryRunner (new ComboPooledDataSource (); Map <Object, Map <String, Object> 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 <Object> 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 <Map <String, Object> 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 <String, Object> 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 (); List <Account> list = runner. query ("select * from account where money>? ", New BeanListHandler <Account> (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> (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 <Object []> 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.
⑧ KeyedHandler (name): encapsulate each row of data in the result set into a Map (List <Map>), 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.


JavalangClassNotFoundException: orgapachecommonsdbutilsResultSetHandler?

Note the class name !!!
 
Code can be used to add, delete, modify, and query the database connected to java

First, you have to determine the form in which your database connection is connected, hibernate or the original jdbc or spring;
If only hibernate is available, you must load the configuration file to get sessionFactory and then get the session
If spring is used, you also need to inject sessionfactory to your dao.
If the jdbc method is used, you should follow the original jdbc Method.
In short, you must have a data source when constructing DAO. In this way, your database can be manipulated.
If you understand these problems, you can solve the first and third problems. As for the second question, I don't understand what you mean!


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.