retrieving the value of a column in a recently-inserted row
Joined: Apr 09, 2002
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 ]
Joined: May 10, 2001
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.