Get Generated Keys From Preparedstatement


  • It shows that you have successfully inserted a new candidate into the candidates table with id 134. In this tutorial, we have shown you how to use PreparedStatement object to insert a new record into a MySQL table and get the inserted ID back for further processing.
  • How to get primary key value (auto-generated keys) from inserted queries using JDBC? Write a simple program for CallableStatement statement to execute stored procedure. Write a program for CallableStatement statement with stored procedure returns OUT parameters. Write a program for CallableStatement statement with batch execution.
  • So you will need to check if your driver actually supports it for batch updates. As indicated in the answer by Philip O., retrieval of generated keys is not supported with batch updates as documented in Oracle 12 JDBC Standards Support: You cannot combine auto-generated keys with batch update.
Re: preparedStatement.getGeneratedKeys() exception.

Retrieving auto-generated keys for an INSERT statement. With the IBM® Data Server Driver for JDBC and SQLJ, you can use JDBC 3.0 methods to retrieve the keys that are automatically generated when you execute an INSERT statement. To retrieve automatically generated keys that are generated by an INSERT statement, you need to perform these steps. Hi, I'm using Derby. How can I retrieve the generated key of an insert statment? Best Regards Thorsten. PreparedStatement.getGeneratedKeys returns null.

Posted by: Mark Matthews
Date: November 05, 2004 03:57PM

> I am not trying to update a row, I am trying to insert a row and get the auto-gen-key back for the
> newly inserted row via getGeneratedKeys() after the pstmt.execute() is called.
> In this case I inserted a new row for dp_objtype and expect to get the objtypeid back in the ResultSet of
> getGeneratedKeys().
Yes, but then you're doing this (according to your prior posts), so I'm confused. You're treating something that obviously isn't an updatable result set like it is:
resultSet = pstmt.getGeneratedKeys();
if (resultSet != null && resultSet.next())
{
newid = resultSet.getInt(1);
resultSet.updateInt(1, newid+1);
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
This result set is not updatable, why are you trying to call updateInt() on it????????
> Maybe my question is not to expect to use the updateable resultset, but why did pstmt.execute()
> expect an updateable resultset when the Connection.preparedStatement(string, return_auto_genkey) is used.
It _doesn't_...Your code is calling ResultSet.updateInt(), which is only valid for updatable result sets. Result sets returned from getGeneratedKeys() are _never_ updatable.
> When I use pstmt returned from Connection.preparedStatement(string), things work;
> but I cannot get the generated key.
That's to be expected, since you didn't tell the driver you wanted them :)
Explain why you want to call ResultSet.updateInt(), and maybe we'll get this figured out, but it appears you think ResultSet.updateInt() is going to do something with a generated key, but I'm still trying to figure out what you believe that 'something' is :(
-Mark

Preparedstatement Get Generated Keys

Options:Reply•Quote

Written By
Re: preparedStatement.getGeneratedKeys() exception.
November 04, 2004 01:26PM
Get generated keys from preparedstatement pdf
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

Generated Keys Preparedstatement

Re: preparedStatement.getGeneratedKeys() exception.
January 03, 2005 09:54PM

Get Generated Keys From Preparedstatement Windows 10


Prepared Statement Get Generated Keys

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

Java Prepared Statement Get Generated Keys

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.