Introduction to DBUtils
Commons DbUtils is an open source tool class library provided by the Apache organization that simply encapsulates JDBC . Using it can simplify the development of JDBC applications without affecting the performance of the program.
DBUtis的API
QueryRunner class: execute sql statement
ResultSetHandler interface: encapsulate the result set into a bean
DBUtils class: tool class closes resource transaction related
Quick Start (Small Case)
Create a user table
create table user(
id varchar(50) primary key,
name varchar(20),
age int,
addr varchar(255),
idcard varchar(20),
account int);
Original jdbc way:
Using DBUtils way:
QueryRunner details
How QueryRunner is created
QueryRunner runner = new QueryRunner();//Manual transaction
QueryRunner runner = new QueryRunner(DataSource);// There are three methods of automatic transaction
QueryRunner : update() batch() query()
update method
Parameters:
Connection: connection object
sql: sql statement (must have)
params: the value of the placeholder position in the sql statement
Method of composition:
update(Connection,sql,params): To manually control the transaction sql, you need to pass in the parameter
update(Connection ,sql): Manually control transaction sql without parameters For example: delete from user;
update(sql,params): Automatically control transaction sql requires parameters
update(sql): Automatically control transaction sql does not require parameters
Automatic transaction execution update
Manual transaction execution update
batch method -- batch
Parameters:
Connection: connection object
sql: sql statement (must have)
params: the value of the placeholder position in the sql statement Two-dimensional array
Method of composition:
batch(Connection,sql,params): all batches if the transaction is rolled back The sql does not take effect
batch(sql,params)
Automatic transaction execution batch
Manual transaction execution batch
query method
Parameters:
Connection: connection object
sql: sql statement (must have)
params: the value of the placeholder position in the sql statement 2D array
ResultSetHandler: encapsulated result set (must have)
Composition method:
query(connection,sql,params,resultsetHandler )
query(connection,sql,resultsetHandler)
query(sql,params,resultsetHandler)
query(sql,resultsetHandler)
ResultSetHandler: encapsulates the result set
handle方法(ResultSet)
custom generic handler
DBUtils provides implementations of 9 ResultSetHandler interfaces
BeanListHandler : The return is a list list encapsulated in entity beans
BeanHandler : The return is the entity bean
ArrayHandler: The return value is an array
ArrayListHandler: The return value is List<Object[]> Each row of data in the result set is encapsulated into an array, and multiple data are put into the collection
MapHandler
MapListHandler
ColumnListHandler: Returns a collection of data whose elements are one column
ScalarHandler : returns the data of the first column of the first row
KeyedHandler:
DBUtils tool class
Mainly close resources and transaction related methods
DBUtils.close(conn)----- conn.close