Problem with getGeneratedKeys
Posted by: Martin Dubuc
Date: April 21, 2005 11:20AM

I am trying to use the getGeneratedKeys function to get an AUTO_INCREMENT key value after insertion. In my current environment, I always get an empty result set (Connector J 3.1.7 and MySQL 5.0.4-beta) even though the row is actually inserted in the database. However, when I use the same Java application, but with MySQL 4.0.23a, the result set is not empty. Is this a problem limited to 5.0.4-beta?

Here is the code:

Statement stmt = con.createStatement(java.sql.ResultSet.TYPE_FORWARD_ONLY, java.sql.ResultSet.CONCUR_UPDATABLE);

updateSQL = "INSERT INTO table (data) VALUES (0)";
stmt.executeUpdate(updateSQL, Statement.RETURN_GENERATED_KEYS);
java.sql.ResultSet rs = stmt.getGeneratedKeys();
if (rs.next())
key = rs.getInt(1);
else
System.err.println("Can't retrieve key.");

Version of Java is 1.4.2.

Options: ReplyQuote


Subject
Written By
Posted
Problem with getGeneratedKeys
April 21, 2005 11:20AM


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

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.