<p>Any advice on how to read auto-incrementing identity field assigned to newly created record from call through <code>java.sql.Statement.executeUpdate</code>?</p>

<p>I know how to do this in SQL for several DB platforms, but would like to know what database independent interfaces exist in <code>java.sql</code> to do this, and any input on people's experience with this across DB platforms.</p>

[{'answer_id': 76292, 'author': 'ScArcher2', 'author_id': 1310, 'author_profile': 'https://Stackoverflow.com/users/1310', 'pm_score': 0, 'selected': False, 'text': "<p>I've always had to make a second call using query after the insert.</p>\n\n<p>You could use an ORM like hibernate. I think it does this stuff for you.</p>\n"}, {'answer_id': 76348, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 6, 'selected': True, 'text': '<p>The following snibblet of code should do ya\':</p>\n\n<pre><code>PreparedStatement stmt = conn.prepareStatement(sql, \n                                 Statement.RETURN_GENERATED_KEYS);\n// ...\n\nResultSet res = stmt.getGeneratedKeys();\nwhile (res.next())\n    System.out.println("Generated key: " + res.getInt(1));\n</code></pre>\n\n<p>This is known to work on the following databases</p>\n\n<ul>\n<li>Derby</li>\n<li>MySQL</li>\n<li>SQL Server</li>\n</ul>\n\n<p>For databases where it doesn\'t work (HSQLDB, Oracle, PostgreSQL, etc), you will need to futz with database-specific tricks.  For example, on PostgreSQL you would make a call to <code>SELECT NEXTVAL(...)</code> for the sequence in question.</p>\n\n<p>Note that the parameters for <code>executeUpdate(...)</code> are analogous.</p>\n'}, {'answer_id': 76377, 'author': 'Alexandre Victoor', 'author_id': 11897, 'author_profile': 'https://Stackoverflow.com/users/11897', 'pm_score': 0, 'selected': False, 'text': '<p>@ScArcher2 : I agree, Hibernate needs to make a second call to get the newly generated identity UNLESS an advanced generator strategy is used (sequence, hilo...)</p>\n'}, {'answer_id': 76381, 'author': 'marcospereira', 'author_id': 4600, 'author_profile': 'https://Stackoverflow.com/users/4600', 'pm_score': 2, 'selected': False, 'text': '<pre><code>ResultSet keys = statement.getGeneratedKeys();\n</code></pre>\n\n<p>Later, just iterate over ResultSet.</p>\n'}, {'answer_id': 76451, 'author': 'Daniel Spiewak', 'author_id': 9815, 'author_profile': 'https://Stackoverflow.com/users/9815', 'pm_score': 0, 'selected': False, 'text': '<p>@ScArcher2</p>\n\n<p>Making a second call is <em>extremely</em> dangerous.  The process of <code>INSERT</code>ing and selecting the resultant auto-generated keys must be atomic, otherwise you may receive inconsistent results on the key select.  Consider two asynchronous <code>INSERT</code>s where they both complete before either has a chance to select the generated keys.  Which process gets which list of keys?  Most cross-database ORMs have to do annoying things like in-process thread locking in order to keep results deterministic.  This is <em>not</em> something you want to do by hand, especially if you are using a database which does support atomic generated key retrieval (HSQLDB is the only one I know of which does not).</p>\n'}]
['https://Stackoverflow.com/questions/76254', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/5446/']