Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use a prepared statement correctly with Spring JDBCTemplate?

I have to do a select query on a database which I will execute often. It works completely fine with a normal statement:

 List<Invoice> l = this.jdbcTemplate.query(
                     "SELECT id, name, stuff FROM example WHERE amount > ?",
                     new Object[] { "100" }, new RowMapper<Invoice>() {...} );

I execute the above statement very frequently, so for performance reason I want to use a prepared statement. However I'm now unsure how I correctly would use the spring API to achieve this.

I'm confused why spring would like me to give an instance of a PreparedStatementCreator as an argument for query? I thought that it's exactly the point that I do not create a new prepared statement every time I use the query method. So I think something along the line of the following snippet would be absolutely pointless as the prepared statement is created newly every time I call the query-method:

 List<Invoice> l = this.jdbcTemplate.query(
                     new PreparedStatementCreator() {
                         String query = "SELECT id, name, stuff FROM example WHERE amount > ?";
                         public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
                              return connection.prepareStatement(query);
                         }
                     },
                     new PreparedStatementSetter() {...}, 
                     new RowMapper<Invoice>() {...} );

Do I have to create my own ReusablePreparedStatementCreator? Which would only create a new method on the first call of createPreparedStatement.

Could the PreparedStatementCreatorFactory help me?

So to rephrase, how would I correctly create a select query that uses a prepared statement with Spring JDBCTemplate to really gain a performance advantage without losing the advantage of the RowMapper?

like image 718
leo Avatar asked May 28 '14 11:05

leo


People also ask

Does spring JdbcTemplate use prepared statements?

The JdbcTemplate will create the PreparedStatement and with the callback only being responsible for setting parameter values. This interface contains one method namely, setValues(PreparedStatement ps): It sets parameter values on the given PreparedStatement.

What is PreparedStatement JdbcTemplate?

PreparedStatementSetter interface acts as a general callback interface used by the JdbcTemplate class. This interface sets values on a PreparedStatement provided by the JdbcTemplate class, for each of a number of updates in a batch using the same SQL.


1 Answers

First of all, I'm not sure you would get a significant performance increase by not recreating the prepared statement every time, because the JDBC driver/database often caches prepared statements.

But if you want to execute multiple queries with a single prepared statement, simply use one of the execute() methods of JdbcTemplate, which creates a prepared statement from a SQL query (or lets you create it), and then executes a callback taking the prepared statement as argument. You can loop inside this callback, and execute the statement as many times you want.

like image 195
JB Nizet Avatar answered Oct 24 '22 21:10

JB Nizet