Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle12c, JDBC, identity and getGeneratedKeys

Tags:

jdbc

oracle12c

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

like image 739
mlbiam Avatar asked Dec 19 '25 01:12

mlbiam


1 Answers

Had the same problem, solution was to tell Oracle which column to return:

String[] generatedKeyColumns = new String[]{"id"};
PreparedStatement ps = connection.prepareStatement(insertStatement, generatedKeyColumns);

like image 67
Moritz Ringler Avatar answered Dec 20 '25 21:12

Moritz Ringler