PreparedStatement 相当于 JDBCTemplate.update(String, Object[])?
所以我一直导致相信 这是使用 JDBCTemplate 从数据库获取自动生成的 ID 值的最有效方法:
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);
我的问题是我经常插入可变数量的值 (JDBCTemplate.update(String, Object[])
实际上正是我所需要的),并且看起来 PreparedStatement
允许一次插入一个(setString
等)。循环遍历数组似乎很……不优雅。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,既然这是一种风滚草,我猜没有其他方法可以实现这一点。我最终创建了一个类来处理这个问题,这样我就可以解决 final 的要求。
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.