I'm trying to do something that works in every DB I have worked with. I want to get the generated keys from an insert. I have oracle 12c setup and have the following table:
CREATE TABLE countyUsers (id integer GENERATED AS IDENTITY,first varchar(255),last varchar(255),email varchar(255),userName varchar(255),unisonFailedLogins number,unisonLastFailedLogin number,unisonLastSuccessLogin number);
Then I run an insert and try to get a generated key using the jdbc thin driver:
ps.executeUpdate();
ResultSet rs = ps.getGeneratedKeys();
if (rs.next()) {
long id = rs.getLong(1);
}
which throws a number format exception. I can get a rowid though. The key coming back is AAAWyHAAGAAAAFNAAA not a number. What am I supposed to do with this?
Thanks
Had the same problem, solution was to tell Oracle which column to return:
String[] generatedKeyColumns = new String[]{"id"};
PreparedStatement ps = connection.prepareStatement(insertStatement, generatedKeyColumns);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With