Apache Commons dbutils User's Manual

Source: Internet
Author: User
Tags repetition wrapper

Apache Commons dbutils User's Manual

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs

First, Introduction


Dbutils is a small JDBC lightweight package, the core of which is a layer of encapsulation on the basis of JDBC, mainly the encapsulation of the result set, can directly encapsulate the result set of the query into JavaBean, which is designed to simplify the confusion and repetition of the JDBC code.
JDBC code Development, there are many difficulties:
1) The operation process is complex, code operation a pattern, a lot of repetition.
2) The result set is difficult to handle.
3) The SqlException is enforced everywhere, affecting the aesthetics and readability of the code.

second, familiar with Dbutils

Before using dbutils, you should be aware of some issues:
1) Dbutils is a simple package for JDBC and can be used in combination with JDBC.
2) dbutils The result set automatically packaged as JavaBean is a demanding requirement:
Must meet the specifications of JavaBean;
Secondly, the name of the bean getter and setter method must correspond to the column name of the result set one by one;
Private members of JavaBean are not required to correspond to table result set column name one by one.
3) Dbutils can encapsulate the result set into various types
There are: bean/list<bean>,map/list<map>/map<map>, array/list< array;, column/list< column;, these types.
For map<map> types using Keyedhandler as the result set processor, the inner map is the "column name-value" pair, and the outer map is the "PRIMARY key-inner map reference", but the primary key here is not necessarily the primary key of the database and can be arbitrarily specified.
4) Dbutils When performing the insert operation, can not return the self-increment primary key, this is a very serious problem, of course, can not blame the dbutils, could be implemented by a workaround, such as in MySQL, after executing an INSERT SQL, then execute select Last_insert_ ID () statement, you can obtain the self-increment primary key.
5) Dbutils Performance and JDBC performance is the same, the testing process did not find performance loss, with high performance, without losing the flexibility of JDBC.

6) for the definition of the member type of JavaBean, there is one rule: use the wrapper type as much as possible instead of using the base type.

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs


Iii. introduction of API

1. Org.apache.commons.dbutils Bag

Dbutils: A small class library for simplifying JDBC operations

Abstractqueryrunner: is the base class for abstract classes, Queryrunner, and Asyncqueryrunner classes.
Asyncqueryrunner: A pluggable way to execute SQL queries and process result sets. is a thread-safe class.
Baseresultsethandler: Transforms the result set into an extension of other objects.
Beanprocessor:beanprocessor matches the column name to the Bean property name and transforms the result set column into the properties of the Bean object.
Dbutils: A collection of JDBC Accessibility tools.
Generousbeanprocessor: Provides an intelligent match between the database column name and the JavaBean property.
Proxyfactory: The proxy implementation that produces the JDBC interface.
Queryloader: Property file loader, which is used primarily to load SQL into memory in the properties file.
Queryrunner: Executes a SQL query and processes the result set using a pluggable policy.
Resultsethandler: A tool to convert resultset to another object.
Resultsetiterator: The wrapper result set is an iterator.
Rowprocessor: A tool that converts resultset rows to other objects.

2. Org.apache.commons.dbutils.handlers Bag

Abstract class of Abstractkeyedhandler:keyedhandler.

Abstractlisthandler: simplifies the abstract class of Resultsethandler class development and transforms the result set into a list.
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 of objects and 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.
Beanmaphandler: Implements the bean return map collection. All rows of the result set are converted to beans and stored in the map according to the specified key.
Columnlisthandler: The data from a column in the result set is stored in the list.
Keyedhandler: Encapsulates each row of data in the result set into a map, and then stores each map in a map based on the specified key.
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.
Scalarhandler: Saves data from one of the columns of a record in the result set as an object.

3. Org.apache.commons.dbutils.wrappers Bag

Sqlnullcheckedresultset: Checks the ResultSet wrapper class for SQL null values on each getxxx method.

Stringtrimmedresultset: Removes the ResultSet wrapper class for the space before and after the string in the result set.

Chszs, reprint need to indicate. Blog home:Http://blog.csdn.net/chszs


Iv. Details of important categories

1, Dbutils class

Dbutils provides a tool class for routine work such as closing connections, loading JDBC drivers, and all of the methods are static.

The main methods are as follows:
1) public static void Close (...) throws SQLException
The Dbutils class provides a three overloaded Close method. These methods check that the supplied arguments are NOT null, and if they are empty, close connection, statement, and ResultSet.
2) public static void closequietly (...)
The Butils class provides four overloaded closequietly methods. This kind of method can not only avoid closing in connection, statement and ResultSet null cases, but also can hide some sqleeception thrown in the program.
3) public static void commitandclosequietly (Connection conn)
Commits SQL within the connection, closes the connection, and does not throw a SQL exception when the connection is closed.
4) public static Boolean loaddriver (String driverclassname)
This method loads and registers the JDBC driver, and if successful returns TRUE, the failure returns false. With this method, you do not need to catch classnotfoundexception exceptions.

2, Queryrunner class

The Queryrunner class simplifies the SQL query, which is combined with Resultsethandler to accomplish most database operations and can reduce the amount of code much.

The Queryrunner class provides two construction methods
1) Default construction method
2) A javax.sql.DataSource is required to construct the parameters.

3. Main methods of Queryrunner class

1) Public Object query (Connection conn, String sql, object[] params, Resultsethandler rsh) throws SQLException

Performs a query operation in which each element value in the object array is used as the permutation parameter for the query statement. This method handles the creation and shutdown of PreparedStatement and resultset on its own.
2) 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 a database connection to the method, and it is re-connection from the data source (DataSource) provided to the constructor method or the Setdatasource method used.
3) Public Object query (Connection conn, String sql, Resultsethandler rsh) throws SQLException
Executes a query operation that does not require a replacement parameter.
4) public int update (Connection conn, String sql, object[] params) throws SQLException
Used to perform an update (INSERT, update, or delete) operation.
5) public int update (Connection conn, String sql) throws SQLException
Used to perform an update operation that does not require displacement parameters.
6) Insert Insertion method
7) Batch processing method





Apache Commons dbutils User's Manual

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.