Hi.
I am using the Jdbc template as my database interface and now I want to read from my database using prepared statements.
In my program I iterate over lots of lines in a csv file and on every line I execute between 3 to 5 sql select queries with it the line's values. These select queries are the same for every line. So that would make 5 prepared statements as a whole.
Currently I sometimes use queryForObject or queryForMap. I'd like to have the same functionality, just with prepared statements to speed up the whole progress.
Now the problem: 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 PreparedStatementSetter. As in this example both of them are created with an 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 the values as a String. I also can't pass them to the PreparedStatementSetter because there are no arguments for the constructor. And setting the variables to final would in my opinion be dumb too.
I was used to the creation of prepared statements being fairly simple like this
as in the Java tutorial. But instead the jdbc template really is a pain so far.Code:PreparedStatement updateSales = con.prepareStatement( "UPDATE COFFEES SET SALES = ? WHERE COF_NAME LIKE ? "); updateSales.setInt(1, 75); updateSales.setString(2, "Colombian"); updateSales.executeUpdate();
I'd want my code to behave something like this.
I hope this pseudo code is helpful for you. Maybe you have some advice for me.Code:PreparedStatement ps = "select NAME, ADDRESS, AGE from TEST where NAME = ?"; LineIterator i = new LineIterator("pathToFile.txt"); while(i.hasNext(){ ps.setString(1, i.getValue()); Object myObject = jdbcTemplate.queryForObject(ps); }
Thank you so far
Bernissimo


Reply With Quote
