11_java _ Cao jianbo 11.28 Apache-DBUtils framework

Source: Internet
Author: User

Apache-DBUtils framework

 

Introduction

Commons-dbutils is an open-source JDBC tool library provided by the Apache organization. It is a simple encapsulation of JDBC and has very low learning costs. In addition, using dbutils can greatly simplify the workload of jdbc coding, at the same time, the program performance will not be affected. Therefore, dbutils has become the first choice for many companies that do not like hibernate.

API introduction:

Org. apache. commons. dbutils. QueryRunner -- BDManager

Org. apache. commons. dbutils. ResultSetHandler-processing result set

Tool

Org. apache. commons. dbutils. DbUtils ,.

DbUtils class

DbUtils: Provides tools such as shutting down connections and loading JDBC drivers. All the methods in the tool are static. The main method is as follows:

Public static void close (...) Throws java. SQL. SQLException: The DbUtils class provides three methods for disabling heavy loads. These methods check whether the provided parameters are NULL. If not, they disable Connection, Statement, and ResultSet.

Public static void closeQuietly (...) : This type of method not only avoids closing when Connection, Statement, and ResultSet are NULL, but also hides SQLEeception thrown in the program.

Public static void commitAndCloseQuietly (Connection conn): Used to submit a Connection and close the Connection. When the Connection is closed, no SQL exception is thrown.

Public static boolean loadDriver (java. lang. String driverClassName): this party loads and registers the JDBC driver. If the driver succeeds, true is returned. With this method, you do not need to catch this exception ClassNotFoundException.

QueryRunner class

This class simplifies SQL queries. Combined with ResultSetHandler, this class can complete most database operations and greatly reduce the coding workload.

The QueryRunner class provides two constructor methods:

Default constructor

A javax. SQL. DataSource is required for parameter construction.

Main method of QueryRunner class

Public Object query (Connection conn, String SQL, Object [] params, ResultSetHandler rsh) throws SQLException: executes a query operation. In this query, each element value in the object array is used as a replacement parameter of the query statement. This method automatically handles the creation and closure of PreparedStatement and ResultSet.

Public Object query (String SQL, Object [] params, ResultSetHandler rsh) throws SQLException: almost the same as the first method. The only difference is that it does not provide database connections to methods, in addition, it re-obtains the Connection from the data source provided to the constructor or the setDataSource method used.

Public Object query (Connection conn, String SQL, ResultSetHandler rsh) throws SQLException: executes a query operation without replacing parameters.

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 without replacing parameters.

ResultSetHandler Interface

 

This interface is used to process java. SQL. ResultSet and convert data to another form as required.

The ResultSetHandler interface provides a separate method: Object handle (java. SQL. ResultSet. rs ).

Implementation class of 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 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.

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, and then store these maps in a map. The key is the specified key.

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 in a Map, and store the data in the List.

Notes

Regardless of the relationship between java objects and relational databases, they all use foreign keys to indicate the associations of records (that is, objects.

Design java objects. If multiple objects are involved in mutual reference, avoid using one-to-many or many-to-many relationships as much as possible, instead, you should use many to one to describe the relationship between objects (or use the delayed Loading Method ).

 

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.