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
-------------------------------------------------------------------------------------------------
-------------------------------------------------------------------------------------------------