[JavaWeb] some interpretations of queryrunner in dbutils

Preface: [this article is an original sharing article, please indicate the source for reprint, thank you.] Some features of dbutils have been described in the previous article. Here we will talk about some internal implementations of queryrunner in detail. Please point out the mistakes

QueryRunner

Queryrunner has three main methods: update () batch (). Let's take a look at the two forms of query. Here we mainly talk about the first method, because we use c3p0 to uniformly manage connection (QueryRunner qr = new QueryRunner(C3P0Utils.getDataSource()))query(sql,ResultSetHandler,Object...params); query(conn,sql,Object...params);

First: params is not required

The second method: a parameter query is required

The third method: multiple parameter query is required

Then let's look at the implementation of the source code: (1) queryrunner java

Interpretation: first, get the connection and use this Preparaconnection() gets The query () method is then invoked to execute the query. Next, let's look at how the source code obtains the currently transmitted connection and the internal implementation of the query () method

It's very simple here. Because we use the c3p0 database connection pool to obtain the datasource, we can go straight to the current connection Next, let's look at the internal implementation of the query () method

Interpretation: it can be seen here that the same query method is called regardless of whether the parameter params is passed. Next, let's look at this fillStatement(stmt,params); How to assign parameters in Preparedstatement

This method is the core The first case: when params is null, return directly and execute the SQL statement The second and the third case: when params is not null, loop through the incoming params, assign params to Preparedstatement, and then fill placeholders for SQL query Here, let's also review how to query directly using Preparedstatement:

2. UPDATE statement

Then queryrunner Update method in Java:

At the time of parameter assignment, the fillstatement method above is called, which will not be described here 3. Batch statement

Then look at the batch () method in queryrunner:

Interpretation: because params is a two-dimensional array, the for loop is used when assigning values to Preparedstatement, and then through Preparedstatement Add batch() for batch addition, and then execute executebatch()

Since it's so late, there are so many queryrunners for the time being. I haven't fully explored the usage of queryrunner. I write it here as a record and exchange 2016/05/24.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>