• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

retrieving the value of a column in a recently-inserted row

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am trying to insert data into 2 tables, but in order to insert into the 2nd table, I need a value from the record inserted into the 1st table, which was not provided by me in my code (it's the primary key of the table, and it's value is determined by the DB, not me)
So, I want to do this:
"insert into users (name, pwd) values (name, password)"
(which isn't a problem), but then I want to retrieve the user_id from the row I just inserted. As far as I know, once I execute an "insert" statement, the ResultSet only contains and int with the number of rows updated, not the actual contents of the table as with a "select."
Is there a way to retrieve this from the ResultSet of the 1st query?
[ July 16, 2002: Message edited by: Brian Kott ]
[ July 16, 2002: Message edited by: Brian Kott ]
 
Ranch Hand
Posts: 3244
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Brian
New to the J2SE 1.4 is an overloading of the Statement.executeUpdate() method. There is now a constant you can add as a secondarguemnt telling it to return any autogenerated columns.

Then after the statment has executed you use the method getGeneratedKeys() on the statement object,this returns a result set with any autogenerated columns in it.
The biggest problem here might be in finding a driver that supports it due to its newness.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic