Using prepared statements with JDBCTemplate
- by Bernhard V
Hi.
I'm using the Jdbc template and want to read from the database using prepared statements. I iterate over many lines in a csv file and on every line I execute some sql select queries with it's values.
Now I want to speed up my reading from the database but I just can't get the Jdbc template to work with prepared statements. Actually I even don't know how to do it.
There is the PreparedStatementCreator and the PreparedStatementCreator. As in this example both of them are created with anonymous inner classes.
But inside the PreparedStatementCreator class I don't have access to the values I want to set in the prepared statement.
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 PreparedStatementCreator because there are no arguments for the constructor.
I was used to the creation of prepared statements being fairly simple. Something like
PreparedStatement updateSales = con.prepareStatement(
"UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? ");
updateSales.setInt(1, 75);
updateSales.setString(2, "Colombian");
updateSales.executeUpdate():
as in the Java tutorial.
Your help would be very appreciated.