Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should use <code>ResultSet#getLong()</code> instead. If in vain, try <code>ResultSet#getRowId()</code> and eventually cast it to <code>oracle.sql.ROWID</code>. If the returned hex string is actually the ID in hexadecimal flavor, then you can try converting it to decimal by <code>Long#valueOf()</code> or <code>Integer#valueOf()</code>.</p> <pre><code>Long id = Long.valueOf(hexId, 16); </code></pre> <p>That said, Oracle's JDBC driver didn't support <code>ResultSet#getGeneratedKeys()</code> for a long time and is still somewhat troublesome with it. If you can't get that right, then you need to execute a <code>SELECT CURRVAL(sequencename)</code> on the same statement as you did the <code>insert</code>, or a new statement inside the same transaction, if it was a <code>PreparedStatement</code>. Basic example:</p> <pre><code>public void create(User user) throws SQLException { Connection connection = null; PreparedStatement preparedStatement = null; Statement statement = null; ResultSet generatedKeys = null; try { connection = daoFactory.getConnection(); preparedStatement = connection.prepareStatement(SQL_INSERT); preparedStatement.setValue(1, user.getName()); // Set more values here. int affectedRows = preparedStatement.executeUpdate(); if (affectedRows == 0) { throw new SQLException("Creating user failed, no rows affected."); } statement = connection.createStatement(); generatedKeys = statement.executeQuery(SQL_CURRVAL); if (generatedKeys.next()) { user.setId(generatedKeys.getLong(1)); } else { throw new SQLException("Creating user failed, no generated key obtained."); } } finally { close(generatedKeys); close(statement); close(preparedStatement); close(connection); } } </code></pre> <p>Oh, from your code example, the following line</p> <pre><code>stmt.RETURN_GENERATED_KEYS; </code></pre> <p>is entirely superfluous. Remove it. </p> <p>You can find <a href="https://stackoverflow.com/questions/1915166/jdbc-how-can-we-get-inserted-record-id-in-java/1915197#1915197">here</a> another example which I posted before about getting the generated keys, it uses the normal <code>getGeneratedKeys()</code> approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
 

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