开发者

PreparedStatement equivalent to JDBCTemplate.update(String, Object[])?

开发者 https://www.devze.com 2023-03-14 14:25 出处:网络
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:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消