gpt4 book ai didi

postgresql - Spring JDBC - 最后插入的 id

转载 作者:行者123 更新时间:2023-11-29 12:39:03 24 4
gpt4 key购买 nike

我正在使用 Spring JDBC。是使用 Spring Framework 获取最后插入的 ID 的简单方法还是我需要使用一些 JDBC 技巧?

jdbcTemplate.update("insert into test (name) values(?)", params, types);
// last inserted id ...

我发现了类似下面的内容,但我得到:org.postgresql.util.PSQLException: Returning autogenerated keys is not supported.

KeyHolder keyHolder = new GeneratedKeyHolder();
jdbcTemplate.update(new PreparedStatementCreator() {

@Override
public PreparedStatement createPreparedStatement(
Connection connection) throws SQLException {
PreparedStatement ps = connection.prepareStatement("insert into test (name) values(?)", new String[] {"id"});
ps.setString(1, "test");
return ps;
}

}, keyHolder);
lastId = (Long) keyHolder.getKey();

最佳答案

旧的/标准的方法是使用 call currval() 在插入( ref )之后。简单且安全。

关于postgresql - Spring JDBC - 最后插入的 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5703281/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com