gpt4 book ai didi

java:在 H2 中的 executeBatch() 之后检索键

转载 作者:塔克拉玛干 更新时间:2023-11-03 04:28:11 29 4
gpt4 key购买 nike

我正在尝试从 executeBatch() 事务中检索生成的 key ,但我只获得了要添加的最后一个 key 。

这是我的代码:

        PreparedStatement ps_insert = conn.prepareStatement(insertQuery, PreparedStatement.RETURN_GENERATED_KEYS);          
for (int i = 0 ; i < adding_dates.length ; i++){
ps_insert.setInt(1, Integer.parseInt(consultant_id));
ps_insert.setDate(2, adding_dates[i]);
ps_insert.setInt(3, Integer.parseInt(room_id));
ps_insert.addBatch();
}
ps_insert.executeBatch();
ResultSet rs = ps_insert.getGeneratedKeys(); //<-- Only the last key retrieved
conn.commit();

我做错了什么?

编辑:很抱歉没有提到我在嵌入式模式下使用 H2 ( http://www.h2database.com/html/main.html) 数据库。

最佳答案

根据H2 jdbc driver javadocs ,这是正常行为:

Return a result set that contains the last generated auto-increment key for this connection, if there was one. If no key was generated by the last modification statement, then an empty result set is returned. The returned result set only contains the data for the very last row.

关于java:在 H2 中的 executeBatch() 之后检索键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14580967/

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