Hi peter,
Basically, server will return different informations to driver. In those informations, there is a "last insert id" that correspond to the first created id of the query, and the number of affected rows.
So for example :
try (Statement stmt = sharedConnection.createStatement()) {
stmt.executeUpdate("INSERT INTO demo (name) VALUES ('Jeff'),('Jones'),('Agnes'),('Wolfgang')", Statement.RETURN_GENERATED_KEYS);
try (ResultSet rs = stmt.getGeneratedKeys()) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
server will return for example "last insert id" 1 and 4 new rows.
Driver will reconstruct the inserted Id according to @@auto_increment_increment.
will return
1
2 (= 1 + @@auto_increment_increment)
3 (= 1 + 2 * @@auto_increment_increment)
4 (= 1 + 3 * @@auto_increment_increment)
In JDBC, standard way is to use PrepareStatement (and it's better to avoid injection than created a String like the previous one) and batch methods.
Example :
String[] names = new String[] {"Jeff", "Jones", "Agnes", "Wolfgang"};
try (PreparedStatement pstmt = sharedConnection.prepareStatement("INSERT INTO demo (name) VALUES (?)", Statement.RETURN_GENERATED_KEYS)) {
for (String name : names) {
pstmt.setString(1, name);
pstmt.addBatch();
}
pstmt.executeBatch();
try (ResultSet rs = pstmt.getGeneratedKeys()) {
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
Server will send to driver informations for each insert in the order corresponding to the addBatch() methods.
There is no reconstruction this way.
I'll add this to documentation next week
diego