Have you got the super easy-to-use JDBC tools?

table of Contents

QueryRunner class

query()

update()  

batch()    

ResultHandler interface

BeanHandler        

BeanListHandler  

MapHandler          

MapListHandler    

case study


Hello, everyone, I’m Little Gray Ape, a programmer who can write bugs,

Today I would like to share with you the specific usage of commonly used tool classes and interfaces for the convenience of data manipulation when using jdbc to connect to the database, and an explanation of Java's jdbc database connection pool technology, interested friends You can read my article " No, no, no, there are people who don’t know the JDBC connection pool technology! ",

In the development of Java database applications, we often perform a series of operations such as adding, deleting, modifying, and checking the data stored in the database. However, if SQL statements are executed according to the traditional Statement and PreparedStatement interfaces, it is not only inconvenient to use, And it will greatly increase the amount of our code, so in actual database operations, we often use the database tool QueryRunner class and ResultHandler interface,

And under normal circumstances, this class and this interface are used in combination, that is, the QueryRunner class processes sql statements, and the ResultHandler interface processes the returned result set.

Then I will share with you their specific usage methods:

First, use the QueryRunner class to import the corresponding jar package,

I am using commons-dbutils-1.6.jar, the official download address of the jar package:

http://commons.apache.org/proper/commons-dbutils/download_dbutils.cgi

 

QueryRunner class

The main function of the QueryRunner class is to process SQL statements. The commonly used methods of this class are as follows:

query()

The Query() method is used for data query. The commonly used parameters in this method are as follows:

conn-the connection used to execute the query.

sql-the query to be executed.

params-replacement parameters.

rsh-The handler that converts the result into an object.

update()  

The Update() method is used to add, delete, and modify. The commonly used parameters in this method are as follows;

conn-the connection used to run the query.

sql-The SQL to be executed.

param-replacement parameter.

batch()    

batch() is used for batch processing, it is used to execute a batch of SQL INSERT, UPDATE or DELETE queries.

The parameters included are:

conn-the connection used to run the query. The caller is responsible for closing this connection.

sql-The SQL to be executed.

params-query replacement parameter array. Each row in this array is a set of batch replacement values.

The return value is the number of rows updated by each statement.

 

ResultHandler interface

The ResultHandler interface is used to process the result set, which can convert the queried result set into a java object, and provides four implementation classes:

BeanHandler        

Map the result set to java objects

BeanListHandler  

Map the result set to a List collection

MapHandler          

Map the result set to a Map set

MapListHandler    

Map the result set to a MapList collection

 

case study

Next, I will show you how to use it with actual cases:

Example 1: Query all the information of the student whose id is 30, and map the result to a Java object

ComboPooledDataSource cds = new ComboPooledDataSource("testc3p0");

Connection connection = cds.getConnection();

String sqlString = "select * from emp_table where id=30";

QueryRunner queryRunner = new QueryRunner();

Student student=queryRunner.query(connection, sqlString, new

BeanHandler(Student.class));    

System.out.println(student.getName());

 

Example 2: Query all the information of the student with the specified name and age, and map the result to a Java object

ComboPooledDataSource cds = new ComboPooledDataSource("testc3p0");

Connection connection = cds.getConnection();

String sqlString = "select * from emp_table where name=? and age=?";

QueryRunner queryRunner = new QueryRunner();

Student student=queryRunner.query(connection, sqlString, new

BeanHandler(Student.class),"李四",22);

System.out.println(student.getName());

 

Example 3: Query a data table, and map all the results in the table into a List collection

ComboPooledDataSource cds = new ComboPooledDataSource("testc3p0");

Connection connection = cds.getConnection();

String sqlString = "select * from emp_table";

QueryRunner queryRunner = new QueryRunner();

List<Student> students=queryRunner.query(connection, sqlString, new

BeanListHandler<Student>(Student.class));

for (Student student : students) {

System.out.println(student.getName());

}

Example 4: Insert specified data into a data table

String sqlString="insert into emp_table(name,age) values (?,?)";

QueryRunner queryRunner = new QueryRunner();

int i = queryRunner.update(connection, sqlString, "赵六",68);

System.out.println(i);

 

Finally, I implemented a simple employee information management system using the QueryRunner class and the ResultHandler interface, which can add, delete, modify, and check basic employee information. The effect is as follows:

Interested friends can download the source code to learn here:

Link: https://pan.baidu.com/s/1-zAT7wAaxzp1ZHN0M_uY0w    Extraction code: lgcr

Regarding the use of JDBC connection database tools, I will share with my friends here. I hope you can criticize and correct any deficiencies.

Feel good, remember to like and follow !

The big bad wolf accompanies you to make progress together!

 

 

Guess you like

Origin blog.csdn.net/weixin_44985880/article/details/110243526