I am trying to get last inserted row id while inserting using JDBC prepared statements. I have an auto increment primary key column as identity column in table. My code is b
The Oracle JDBC documentation says:
If key columns are not explicitly indicated, then Oracle JDBC drivers cannot identify which columns need to be retrieved. When a column name or column index array is used, Oracle JDBC drivers can identify which columns contain auto-generated keys that you want to retrieve. However, when the
Statement.RETURN_GENERATED_KEYS
integer flag is used, Oracle JDBC drivers cannot identify these columns. When the integer flag is used to indicate that auto-generated keys are to be returned, theROWID
pseudo column is returned as key. TheROWID
can be then fetched from theResultSet
object and can be used to retrieve other columns.
You aren't specifying the columns (as shown in their sample code) so you're retrieving the ROWID
; trying to get that with getInt()
is causing the error you see. (I actually see Invalid column type: getInt not implemented for class oracle.jdbc.driver.RowidAccessor ERROR CODE: 17004
but I think that's down to a different driver version).
You need to specify the column that gets the auto-generated value. If it is called MEETING_ID
then you'd do:
String returnCols[] = { "MEETING_ID" };
preparedStatement = dbConnection.prepareStatement(insertTableSQL, returnCols);
... passing the array of columns - only one in this case - rather than the RETURN_GENERATED_KEYS flag.
The rs.getInt(1)
will then be retrieving that numeric value.