DbUtils (1) result set overview and dbutils result Overview

Source: Internet
Author: User

DbUtils (1) result set overview and dbutils result Overview

Record your learning and understanding of DbUtils

I feel that the most common use of Dbutils is to process the query result set, so I will start to understand the Dbutils library.

The conversion of the source code discovery result set is mainly used for the query, insert, and insertBatch methods.

// Only list the relevant code of the QueryRunner class, others skipped // ----------- source code 1 query method ------------------ stmt = this. prepareStatement (conn, SQL); this. fillStatement (stmt, params); rs = this.wrap(stmt.exe cuteQuery (); result = rsh. handle (rs); // processing result set // ----------- source code 2 insert method ------------------ stmt = conn. prepareStatement (SQL, tatement. RETURN_GENERATED_KEYS );
This. fillStatement (stmt, params); stmt.exe cuteUpdate (); ResultSet resultSet = stmt. getGeneratedKeys (); generatedKeys = rsh. handle (resultSet); // processing result set // ------------- source code 3 insertBatch method ---------------- ResultSet rs = stmt. getGeneratedKeys (); generatedKeys = rsh. handle (rs); // process the result set

The conversion of ResultSet mainly involves two interfaces (ResultSetHandler <T>AndRowProcessor).

1,ResultSetHandler <T>,The diagram is as follows:

The ResultSetHandler interface has only one handle (ResultSet rs) method to be implemented. In my understanding, this method is an entry for processing the result set, each result set conversion class processes its own tasks in its own handle method. This can be seen from the previous source code 1, 2, and 3. It is used to call the handle method of the implementation class, then return the result.

The implementation class of ResultSetHandler (result set conversion class) implements processing of the result set in the handle method. Most of the operations involve another interface.RowProcessorBasicRowProcessor.

2,RowProcessor, the relationship diagram is as follows:

The RowProcessor interface has four methods to implement. The specific usage can be intuitively seen from the method name. These methods are called in the handle method of the result conversion class. Note that if you need to convert the result set to Bean, the use of BeanProcessor class will also be involved.

3,ResultSetHandler andRowProcessor relationship

Although the figure is a bit messy, you can see it clearly. Most result sets hold a reference to the RowProcessor interface. Note that ArrayHandler is a class, because references to RowProcessor of other classes are obtained from this class. This class creates the RowProcessor implementation class BasicRowProcessor object.

Note that the three abstract classes (AbstractKeyedHandler, AbstractListHandler, and BaseResultSetHandler) can be used as result conversion classes. According to the instructions in the document, if none of the officially provided conversion classes meet your requirements, you can implement your own result set conversion class by inheriting BaseResultSetHandler.

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.