Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing prepared statements with JDBCTemplate
    text
    copied!<p>I'm using the JDBC template and want to read from a database using prepared statements. I iterate over many lines in a .csv file, and on every line I execute some SQL select queries with corresponding values.</p> <p>I want to speed up my reading from the database but I don't know how to get the JDBC template to work with prepared statements. </p> <p>There is the <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/PreparedStatementCreator.html" rel="noreferrer">PreparedStatementCreator</a> and the <a href="http://static.springsource.org/spring/docs/2.0.x/api/org/springframework/jdbc/core/PreparedStatementSetter.html" rel="noreferrer">PreparedStatementSetter</a>. As in <a href="http://www.java2s.com/Code/Java/Spring/PreparedStatementCreatorAndPreparedStatementSetter.htm" rel="noreferrer">this example</a> both of them are created with anonymous inner classes. But inside the PreparedStatementSetter class I don't have access to the values I want to set in the prepared statement. </p> <p>Since I'm iterating through a .csv file, I can't hard code them as a String because I don't know them. I also can't pass them to the PreparedStatementSetter because there are no arguments for the constructor. And setting my values to final would be dumb too.</p> <p>I was used to the creation of prepared statements being fairly simple. Something like </p> <pre><code>PreparedStatement updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? "); updateSales.setInt(1, 75); updateSales.setString(2, "Colombian"); updateSales.executeUpdate(): </code></pre> <p>as in this <a href="http://java.sun.com/docs/books/tutorial/jdbc/basics/prepared.html" rel="noreferrer">Java tutorial</a>.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload