Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>This is not consistent with other databases but, when using Oracle, <code>getGeneratedKeys()</code> returns the ROWID for the inserted row when using <code>Statement.RETURN_GENERATEDKEYS</code>. So you need to use the <code>oracle.sql.ROWID</code> proprietary type to "read" it:</p> <pre><code>Statement stmt = connection.createStatement(); stmt.executeUpdate(insertCmd, Statement.RETURN_GENERATED_KEYS); ResultSet rs = stmt.getGeneratedKeys(); oracle.sql.ROWID rid = (oracle.sql.ROWID) rs.getObject(1); </code></pre> <p>But this won't give you the generated ID of the PK. When working with Oracle, you should either use the method <a href="http://java.sun.com/javase/6/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String,%20int[])" rel="nofollow noreferrer"><code>executeUpdate(String sql, int[] columnIndexes)</code></a> or <a href="http://java.sun.com/javase/6/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String,%20java.lang.String[])" rel="nofollow noreferrer"><code>executeUpdate(String sql, String[] columnNames)</code></a> instead of <a href="http://java.sun.com/javase/6/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String,%20int)" rel="nofollow noreferrer"><code>executeUpdate(String sql, int autoGeneratedKeys)</code></a> to get the generated sequence value. Something like this (adapt the value to match the index or the name of your primary key column):</p> <pre><code>stmt.executeUpdate(INSERT_SQL, new int[] {1}); ResultSet rs = stmt.getGeneratedKeys(); </code></pre> <p>Or </p> <pre><code>stmt.executeUpdate(INSERT_SQL, new String[] {"ID"}); ResultSet rs = stmt.getGeneratedKeys(); </code></pre> <p>While digging a bit more on this, it appears that this approach is shown in the <a href="http://static.springsource.org/spring/docs/2.5.x/reference/jdbc.html#jdbc-auto-genereted-keys" rel="nofollow noreferrer">Spring documentation</a> (as mentioned <a href="http://www.kazed.net/index.php/home/36-java-persistence/49-db2-generated-id.html" rel="nofollow noreferrer">here</a>) so, well, I guess it can't be totally wrong. But, unfortunately, it is not really portable and it may not work on other platforms.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload