|
I made some tests using plain JDBC code. It is not really straightforward to retrieve the keys, you need to specify the name of the key field or fields to the executeUpdate method. Here's a little snippet of working code for the scenario I described above:
Connection connection = dataSource.getConnection();
Statement statement = connection.createStatement();
statement.executeUpdate("insert into Company (name) values (12)", new String[]{"id"});
ResultSet rs = statement.getGeneratedKeys();
if (rs.next()) {
Integer key = rs.getInt(1);
System.out.println("GENERATED KEY: " + key);
} else {
System.out.println("No keys retrieved :(");
}
If you use the simplest version of the executeUpdate method you'll get a type conversion error similar to the one I posted when trying to access the keys resultset.
I hope this helps.
|