Why Invalid conversion requested ERROR CODE: 17132?

前端 未结 1 1204
栀梦
栀梦 2021-01-18 01:16

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

1条回答
  •  离开以前
    2021-01-18 01:26

    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, the ROWID pseudo column is returned as key. The ROWID can be then fetched from the ResultSet 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.

    0 讨论(0)
提交回复
热议问题