How to get value of a primary key as soon as you inserted it

Here’s an article that talks about how : https://www.ibm.com/developerworks/java/library/j-jdbcnew/

And here’s a code snippet:

PreparedStatement ps = conn.prepareStatement(sqlCode,
     PreparedStatement.RETURN_GENERATED_KEYS);

……

ps.executeUpdate();
   //Get the value of the key inserted by this statement
   ResultSet rs = ps.getGeneratedKeys();
   if ( rs.next() ) {
       // Retrieve the auto generated key(s).
       int key = rs.getInt(1);
       pOrder.setOrderId(key);
   }

Advertisements
This entry was published on January 20, 2009 at 3:18 pm and is filed under Database, Java/J2EE. Bookmark the permalink. Follow any comments here with the RSS feed for this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: