Difference between RETURN_GENERATED_KEYS and Specifying Generated Column Names

What is the difference between prepareStatement(String sql, int autoGeneratedKeys)i prepareStatement(String sql, String[] columnNames) JDBC class Connection?

Javadoc for both indicates that the returned object PreparedStatementis capable of returning automatically generated keys if the SQL statement is a statement INSERT. In the case of the first API, Statement.RETURN_GENERATED_KEYSyou must pass for the parameter autoGeneratedKeys. In the case of the second API, the names of the generated columns are passed as a string array.

What are the reasons for using one over the other?

I noticed that the Spring class SimpleJdbcInsertprefers the option where the column names are indicated:AbstractJdbcInsert.prepareStatementForGeneratedKeys

Why is this?

+4
source share
1 answer

- , , . , , , all Statement.RETURN_GENERATED_KEYS.

, :

  • ,
  • , , .

, PostgreSQL RETURNING * ( 1), Firebird ( ) .

, (, Oracle - ? - ROWID, , ), , , , , (!).

prepareStatement(String sql, String[] columnNames) prepareStatement(String sql, int[] columnIndexes) ( ), , . , , , , RETURN_GENERATED_KEYS.

, String[] columnNames, , , , int[] columnIndexes - .

+5

All Articles