JAVA uses DBUtils to operate the database

Abstract: This article mainly learns how to use DBUtils to operate the database more conveniently in Java code.

Overview

DBUtils is a practical tool for database operation in Java programming, compact, simple and practical.

DBUtils encapsulates JDBC operations, simplifies JDBC operations, and can write less code.

use

ready

If you need to use the DBUtils tool class, you need to import the package:

commons-dbutils-1.7.jar

DBUtils encapsulates the operation of the database after the connection is established, and mainly has three core functions:

1) The QueryRunner class provides APIs for SQL statement operations.

2) The ResultSetHandler interface is used to define how to encapsulate the result set after the query operation.

3) The DBUtils tool class defines methods for closing resources and transaction processing.

Common method

Construction method

Provides two construction methods with and without data source.

 public QueryRunner();// 不提供数据源,需要手动维护Connection。
 public QueryRunner(DataSource ds);// 提供数据源,DbUtils底层自动维护连接Connection。

Query operation

Supports query operations.

 public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh);
 public <T> T query(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params);
 public <T> T query(String sql, ResultSetHandler<T> rsh);
 public <T> T query(String sql, ResultSetHandler<T> rsh, Object... params);

Update operation

Support the execution of add, modify, and delete operations.

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

Add operation

Supports the execution of adding operations, and can return the added data.

 public <T> T insert(Connection conn, String sql, ResultSetHandler<T> rsh);
 public <T> T insert(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params);
 public <T> T insert(String sql, ResultSetHandler<T> rsh);
 public <T> T insert(String sql, ResultSetHandler<T> rsh, Object... params);

Batch execution

Support batch execution of adding, modifying, and deleting operations.

 public int[] batch(Connection conn, String sql, Object[][] params);
 public int[] batch(String sql, Object[][] params);

Stored procedure

Supports the execution of the procedure statement with the error, and also supports the statement without the stored procedure, but it is not recommended to call this method for the statement without the procedure with the error.

 public int execute(Connection conn, String sql, Object... params);
 public int execute(String sql, Object... params);
 public <T> List<T> execute(Connection conn, String sql, ResultSetHandler<T> rsh, Object... params);
 public <T> List<T> execute(String sql, ResultSetHandler<T> rsh, Object... params);

ResultSetHandler interface description

  • 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 then store it in the List.
  • ArrayHandler: Convert the first row of data in the result set into an object array.
  • ArrayListHandler: Convert each row of data in the result set into an object array, and then store it in the List.
  • MapHandler: Encapsulate the first row of data in the result set into a Map, where the key is the column name, and the value is the corresponding value.
  • MapListHandler: encapsulate each row of data in the result set into a Map, and then store it in the List
  • ScalarHandler: Put a column of the first row of the result set into an object.
    Close resources and transaction processing

Whether to automatically close the Connection resource is determined by the constructor used when creating the QueryRunner:

1) If the data source is passed in, the Connection will be automatically closed, and there is no need to pass in the Connection when calling the method of adding, deleting, checking and modifying. This method will cause a new connection to be established every time the SQL is executed, and the connection will be disconnected after the SQL is executed, and the transaction cannot be controlled through the Connection.

2) If there is no incoming data source, the Connection needs to be closed manually, and the Connection needs to be manually passed in when calling the addition, deletion, and modification method. In this way, new connections will not be obtained when SQL is executed, and the connection will not be disconnected after SQL execution is completed, and transactions can be controlled based on the incoming Connection.

Some high-frequency interview questions collected in the latest 2020 (all organized into documents), there are many dry goods, including mysql, netty, spring, thread, spring cloud, jvm, source code, algorithm and other detailed explanations, as well as detailed learning plans, interviews Question sorting, etc. For those who need to obtain these contents, please add Q like: 11604713672

Guess you like

Origin blog.csdn.net/weixin_51495453/article/details/113854740