I have a (MySQL) batch insert scenario where I need to retrieve all the generated keys. What is the best approach for this? Can I take advantage of the new "return generated keys" feature for this?

Here are three approaches I can think of. I haven't tried any of them yet. Figured someone here might save me the trouble of trying all these one at a time.

1) could be painfully slow, but seems likely to work, and would yield the best error handling/recovery possibilities:

ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for (int x = 0; x < n; x++) {
 ps.setXXXX(1, var[x]);
 checkResult(ps.executeUpdate());
 rs = ps.getGeneratedKeys();
 if (rs.next()) {
   list.add(rs.getXXXX(1));
 }
}


2) Would be somewhat faster and would still enable good error handling/recovery; will this work?


ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for (int x = 0; x < n; x++) {
 ps.setXXXX(1, var[x]);
 checkResult(ps.executeUpdate());
}
rs = ps.getGeneratedKeys();
while (rs.next()) {
 list.add(rs.getXXXX(1));
}


3) Would be fastest, but would sacrifice flexibility in error handling/recovery (possibly); will this work?


ps = c.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
for (int x = 0; x < n; x++) {
 ps.setXXXX(1, var[x]);
 ps.addBatch();
}
checkResults(ps.executeBatch());
rs = ps.getGeneratedKeys();
while (rs.next()) {
 list.add(rs.getXXXX(1));
}


Thanks, Erik



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]



Reply via email to