Apache-dbutils Framework Introduction, Dbutils class, Queryrunner class, Resultsethandler interface

Source: Internet
Author: User


Apache-dbutils Framework Introduction, Dbutils class, Queryrunner class, Resultsethandler interface

Commons-dbutils is an open-source JDBC Tool class library provided by the Apache organization, a simple encapsulation of JDBC, very low learning costs, and the use of dbutils can greatly simplify the workload of JDBC coding without affecting program performance. So Dbutils became the first choice for a lot of companies that don't like hibernate.

API Introduction:

Org.apache.commons.dbutils.QueryRunner--bdmanager

org.apache.commons.dbutils.resultsethandler-Processing Result Sets

Tool class

Org.apache.commons.dbutils.DbUtils,.

Dbutils class

Dbutils: A tool class that provides routine work such as closing connections, loading JDBC drivers, and all of the methods inside are static. The main methods are as follows:

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

public static void Closequietly (...): This kind of method can not only avoid the shutdown in connection, statement and ResultSet, but also hide some sqleeception thrown in the program.

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

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

Queryrunner class

This class simplifies SQL queries, which are combined with Resultsethandler to do most of the database operations and greatly reduce the amount of coding.

The Queryrunner class provides two construction methods:

Default method of Construction

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

The main methods of the Queryrunner class

Public Object Query (Connection conn, String sql, object[] params, Resultsethandler rsh) throws SQLException: Perform a query operation, in this check In the query, each element value in the object array is used as the permutation parameter for the queried statement. This method handles the creation and shutdown of PreparedStatement and ResultSet itself.

Public Object query (String sql, object[] params, Resultsethandler rsh) throws SQLException: Almost as the first method The only difference is that it does not provide a database connection to the method, and it is Connection from the data source (DataSource) provided to the construction method or the Setdatasource method used.

Public Object Query (Connection conn, String sql, Resultsethandler rsh) throws SQLException: Performs a query operation that does not require a substitution parameter.

public int update (Connection conn, String sql, object[] params) throws SQLException: Used to perform an update (INSERT, update, or delete) operation.

public int update (Connection conn, String sql) throws SQLException: Used to perform an update operation that does not require a substitution parameter.

Resultsethandler interface

This interface is used to process java.sql.ResultSet, converting data to another form as required.

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

Implementation classes for Resultsethandler interfaces

Arrayhandler: Turns the first row of data in the result set into an array of objects.

Arraylisthandler: Each row of data in the result set is converted into an array and stored in the list.

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

Beanlisthandler: Encapsulates each row of data in the result set into a corresponding JavaBean instance, which is stored in the list.

Columnlisthandler: Stores data from a column in the result set into a list.

Keyedhandler (name): Encapsulates each row of data in the result set into a map, and then saves the map to a map with the key as the specified key.

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

Maplisthandler: Encapsulates each row of data in a result set into a map and then stores it in the list

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.