获取jdbc执行批处理的生成密钥

时间:2014-08-28 09:26:29

标签: java jdbc

我想在jdbc批量更新中获取所有生成的密钥,用于在数据库中存储arraylist值 我想在pmu.setId();

中设置生成的值

我正在使用Mysql数据库和驱动程序mysql-connector-java 5.1.14

我的代码是:

ps1 = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for (PostMessageUsers pmu : pmuList) {
                    ps1.setLong(1, pm.getMessageid());
                    ps1.setLong(2, pmu.getAtomByReceivedBy().getId());
                    ps1.setLong(3, pmu.getAtomBySentBy().getId());
                    ps1.setLong(4, pmu.getAtomByAuthor().getId());
                    ps1.addBatch();
                    if (++batchCount % BATCH_SIZE == 0) {
                        ps1.executeBatch();
                        res = ps.getGeneratedKeys();
                        if (res.next()) {
                            pmu.setId((long) res.getInt(1));
                        }
                    }
                }
                int postMessageUsers[] = ps1.executeBatch();
            }

我可以在if (++batchCount % BATCH_SIZE == 0) {

中获得相应的ID

但我想知道如何为最后int postMessageUsers[] = ps1.executeBatch();获取gererated id 如果上面的语句执行,我怎么能为此获得生成的id。

0 个答案:

没有答案