Java Get Generated Keys Prepared Statement Mysql

If generated keys are requested on a table that has no auto increment column, the JDBC driver will return a null result set. When you insert rows by executeUpdate or execute an INSERT statement or an INSERT within SELECT statement, you need to indicate that you will want to retrieve automatically generated key values. This array contains the indexes of the columns in the target table that contain the auto-generated keys that should be made available. The driver will ignore the array if the SQL statement is not an INSERT statement, or an SQL statement able to return auto-generated keys (the list of such statements.

28.7.28.3 How to Get the Unique ID for the Last Inserted Row

If you insert a record into a table that contains an AUTO_INCREMENT column, you can obtain the value stored into that column by calling the mysql_insert_id() function.

Setting it to true using a debugger solves the issue for me. The flag is set to true in various methods, including e.g. Com.mysql.cj.jdbc.StatementImpl.executeInternal, but it seems the flag is only ever set for static statements, not prepared statements. This is fixed for 3.0.13, but only for prepared statements (it is a little outside the scope of the JDBC spec, as there is no corrolary executeBatch method in java.sql.Statement that specifies that generated keys should be returned). Spring provides some useful support for this operation and the reference guide seems to answer your question. There is not a standard single way to create an appropriate PreparedStatement (which explains why the method signature is the way it is). Generate ssh public and private keys.

You can check from your C applications whether a value was stored in an AUTO_INCREMENT column by executing the following code (which assumes that you've checked that the statement succeeded). It determines whether the query was an INSERT with an AUTO_INCREMENT index:

When a new AUTO_INCREMENT value has been generated, you can also obtain it by executing a SELECT LAST_INSERT_ID() statement with mysql_query() and retrieving the value from the result set returned by the statement.

When inserting multiple values, the last automatically incremented value is returned.

For LAST_INSERT_ID(), the most recently generated ID is maintained in the server on a per-connection basis. It is not changed by another client. It is not even changed if you update another AUTO_INCREMENT column with a nonmagic value (that is, a value that is not NULL and not 0). Using LAST_INSERT_ID() and AUTO_INCREMENT columns simultaneously from multiple clients is perfectly valid. Each client will receive the last inserted ID for the last statement that client executed.

If you want to use the ID that was generated for one table and insert it into a second table, you can use SQL statements like this:

mysql_insert_id() returns the value stored into an AUTO_INCREMENT column, whether that value is automatically generated by storing NULL or 0 or was specified as an explicit value. LAST_INSERT_ID() returns only automatically generated AUTO_INCREMENT values. If you store an explicit value other than NULL or 0, it does not affect the value returned by LAST_INSERT_ID().

For more information on obtaining the last ID in an AUTO_INCREMENT column:

  • For information on LAST_INSERT_ID(), which can be used within an SQL statement, see Section 12.15, “Information Functions”.

  • For information on mysql_insert_id(), the function you use from within the C API, see Section 28.7.6.38, “mysql_insert_id()”.

  • For information on obtaining the auto-incremented value when using Connector/J, see Retrieving AUTO_INCREMENT Column Values through JDBC.

  • For information on obtaining the auto-incremented value when using Connector/ODBC, see Obtaining Auto-Increment Values.


Posted by: Alex Cheung
Date: November 04, 2004 11:39AM

[java] java.lang.NullPointerException
[java] at com.mysql.jdbc.Statement.getGeneratedKeys(Statement.java:308)
--- details below ---
MySql version: 4.0.21-nt
MySql JConnector Driver version: 3.0.15-ga
table:
create table dp_objtype
(
objtypeid int not null auto_increment,
objname varchar(64) not null,
objclass varchar(255) null,
PRIMARY KEY (objtypeid)
) type = innoDB;
statement:
PreparedStatement pstmt = conn.preparedStatement('insert into dp_objtype (objname, objclass) values (?,?)', Statement.RETURN_GENERATED_KEYS);
If I use pstmt.execute(), the insert-stmt executed without any problem. If I use pstmt.getGeneratedKeys(), I get the following Exception:
[java] psqlstmt = insert into dp_objtype (objname, objclass) values (?,?)
[java] set val[1] = user
[java] set val[2] = com.datapipes.db.User
[java] java.lang.NullPointerException
[java] at com.mysql.jdbc.Statement.getGeneratedKeys(Statement.java:308)
[java] at com.mysql.jdbc.PreparedStatement.getGeneratedKeys(PreparedSta
tement.java:538)
[java] at com.ace.db.DbHelper.insertObject(DbHelper.java:351)
If someone can tell me how to correct this, or if there is a fix for it somewhere.
thanks
-alex

Options:Reply•Quote

Written By
Re: preparedStatement.getGeneratedKeys() exception.
November 04, 2004 01:26PM
Re: preparedStatement.getGeneratedKeys() exception.
November 04, 2004 04:07PM
Re: preparedStatement.getGeneratedKeys() exception.
November 04, 2004 08:19PM
Re: preparedStatement.getGeneratedKeys() exception.
November 05, 2004 01:07AM
Re: preparedStatement.getGeneratedKeys() exception.
November 05, 2004 09:39AM
Re: preparedStatement.getGeneratedKeys() exception.
November 05, 2004 11:13AM
Re: preparedStatement.getGeneratedKeys() exception.
November 05, 2004 03:57PM
Re: preparedStatement.getGeneratedKeys() exception.
November 06, 2004 02:19AM
Re: preparedStatement.getGeneratedKeys() exception.
January 03, 2005 09:54PM

Sorry, you can't reply to this topic. It has been closed.

Java Get Generated Keys Prepared Statement Mysql Download

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.