File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JDBC and the fly likes retrieving the value of a column in a recently-inserted row Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Reply Bookmark "retrieving the value of a column in a recently-inserted row" Watch "retrieving the value of a column in a recently-inserted row" New topic
Author

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

Brian Kott
Greenhorn

Joined: Apr 09, 2002
Posts: 1
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 ]
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
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.


Dave
 
I agree. Here's the link: http://zeroturnaround.com/jrebel/download
 
subject: retrieving the value of a column in a recently-inserted row
 
Similar Threads
inserting data to a table
Sequence number in oracle using Entity Bean
complex one-to-many
Bizzare behavior of Oracle
hyperlink