简体   繁体   中英

PreparedStatement.RETURN_GENERATED_KEYS not working with batch insert

Prepared Statement with return generated keys is returning the auto generated key for single insert but not working for batch insert. Is there a way to get the auto generated values during the insertion itself.

    PreparedStatement ps = connection.prepareStatement("insert into table() values()",PreparedStatement.RETURN_GENERATED_KEYS);
    {
    ps.addBatch();
    }
    ps.executeBatch();
    ResultSet rs = ps.getGeneratedKeys();
    List<Long> ids = new ArrayList<Long>();
    while(rs.next()) {
    ids.add(rs.getLong(1));
    }

Is there a way to get the auto generated values during the insertion itself.

No. After you ps.executeBatch() you need to call ps.getGeneratedKeys() and loop through the ResultSet it returns.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM