So I have been lead to believe that this is the most efficient way of getting an auto-generated ID value from a database using a JDBCTemplate:
KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(
new PreparedStatementCreator() {
public PreparedStatement createPreparedStatement(Connection connection) throws SQLException {
PreparedStatement ps =
connection.prepareStatement(INSERT_SQL, new String[] {"开发者_如何转开发ID_FIELD"});
// Configure the PreparedStatement HERE!
return ps;
}
},
keyHolder);
My problem is that I'm often inserting a variable number of values (JDBCTemplate.update(String, Object[])
is actually exactly what I need), and it looks like PreparedStatement
allows insertion of one at a time (setString
and the like). Looping through the array seems to be so... inelegant.
Well, since this is a tumbleweed, I'm guessing that there are no other ways to accomplish this. I ended up creating a class to handle this so that I can get around the requirement for final.
精彩评论