Apache--dbutils Framework Resultsethandler Interface uses

Source: Internet
Author: User

Reference Link: http://i.cnblogs.com/EditPosts.aspx?opt=1

Brief introduction

Commons-dbutils is an open source JDBC Tool class library provided by Apache, which is a simple encapsulation of JDBC, very low learning costs, and the use of dbutils can greatly simplify the work of JDBC coding without compromising the performance of the program. As a result, Dbutils is the first choice for many companies that do not like hibernate.
API Introduction:
Org.apache.commons.dbutils.QueryRunner---Core
Org.apache.commons.dbutils.ResultSetHandler
Tool class
Org.apache.commons.dbutils.DbUtils.

Dbutils class

Dbutils: A tool class that provides general work such as closing a connection, loading a JDBC driver, and all of the methods are static . The main methods are as follows:

The public static void close (...) throws Java.sql.SQLException:DbUtils class provides three overloads for the shutdown method. These methods check that the supplied arguments are null, and if not, they close connection, statement, and ResultSet.

public static void closequietly (...) : This class of methods not only shuts down connection, statement, and ResultSet, but also hides some of the sqlexception thrown in the program.

public static void commitandclosequietly (Connection conn): Used to commit the connection, then close the connection, and no SQL exception is thrown when the connection is closed.

public static Boolean loaddriver (Java.lang.String driverclassname): This method loads and registers the JDBC driver and returns true if successful. Using this method, you do not need to catch this exception classnotfoundexception.

Queryrunner class

This class simplifies the SQL query, which is combined with Resultsethandler to accomplish most of the database operations and greatly reduces the number of encodings.

The Queryrunner class provides two methods of construction:

The default constructor method

A javax.sql.DataSource is required to construct a parameter.

The main methods of the Queryrunner class

Update action

public int update (Connection conn, String sql, Object...params)

public int update (String sql,object...params)

Query operations

Public Object Query (Connection conn, String sql, Resultsethandler rsh, Object...params)

Public Object query (String sql, Resultsethandler rsh, Object...params)

Resultsethandler interface

This interface is used to process java.sql.ResultSet and convert data to another form on demand.

The Resultsethandler interface provides a separate method: Object handle (java.sql.ResultSet.rs).

Implementation class for the Resultsethandler interface

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 array of objects, which is then stored in the list.

Beanhandler: encapsulates the first row of data in the result set into a corresponding JavaBean instance.

Beanlisthandler: each row of data in the result set is encapsulated in a corresponding JavaBean instance and stored in the list.

Maphandler: Encapsulates the first row of data in the result set into a map, where key is the column name, and value is the corresponding value.

Maplisthandler: Each row of data in the result set is encapsulated in a map and then stored in the list.

Columnlisthandler: The data from a column in the result set is stored in the list.

Keyedhandler (name): Encapsulates each row of data in the result set into a map (List) and thensaves the map to a map with the specified column key.

Scalarhandler: Gets the value of the first row of data specified column in the result set, commonly used for single-value queries

Such as: long Long = (long) queryrunner.query ("SELECT count (*) from account", New Scalarhandler (1));//Single value query


-------------------------------------------------------------------------------------------------

-------------------------------------------------------------------------------------------------


Apache--dbutils Framework Resultsethandler Interface uses

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.