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.
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.
There is the PreparedStatementCreator and the PreparedStatementSetter. As in this example 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.
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.
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 this Java tutorial.
I’ve tried a select statement now with a
PreparedStatement, but it turned out that it was not faster than the Jdbc template. Maybe, as mezmo suggested, it automatically creates prepared statements.Anyway, the reason for my sql
SELECTs being so slow was another one. In theWHEREclause I always used the operatorLIKE, when all I wanted to do was finding an exact match. As I’ve found outLIKEsearches for a pattern and therefore is pretty slow.I’m using the operator
=now and it’s much faster.