Oh i see what you mean - you want to use the first technique. Have you tried just getting the auto-inc value from the ResultSet directly with rs.getInt("autoincfieldnam
Main Topics
Browse All TopicsUsing JDBC's java.sql.ResultSet, I write a new record and want to get the automatically-generated key for the record that was just written. Is there a way to do that?
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
The db/driver "supporting it" is just a special way MySql works. it's not a standard to "goto last record" to get the just inserted record.
So that leaves the original question again!
Either I have to switch to the prepared-statement "insert", or find some link between the resultset and the corresponding update statement, if there is such a thing. Perhaps JDBC definition/standard doesn't support getting the inserted keys from resultset.insertRow.
Let's see if other people have any experience with this!
I'm using Derby right now, but want to be able to support other databases as well, maybe MySql, SqlServer, Postgres, etc.
Perhaps a better solution would be to take the ResultSet and write it out using a prepared statement (then discard the 'insertRow' ResultSet). I could get the assigned key reliably. (I read a benchmark for Derby that writing using prepared statements is slower than using ResultSet).
I assume the code for ResultSet.insertRow would be dependent on each JDBC driver. (It seems it could be built on top of JDBC's prepared statements; if so, I could look at the code to see if I could make something similar).
I read something vaguely about a RowSet, maybe a temporary buffer for storing field values. If there is such a thing, maybe I could use that instead of inventing another wheel.
Actually, I read that writing with ResultSet is slower.
I wanted to use ResultSet because a lot of the work was already done, with the field names, field list, setting and getting, etc., but I suppose I can reinvent another wheel. Many other people should have come across this issue, I wonder if there is a simple solution.
I'm sorry, I meant writing with prepared statements was slower than with resultset (by about 20 to 30 %), but it seems you read the opposite!
I can look into using hibernate. The reasons I stayed away:
1. another layer to learn,
2. not sure if it can do things like scroll through a result set
3. not sure about its overhead/speed.
For most simple gui operations, the speed doesn't matter. But for batch processing of 500k+ records, it may.
4. I assume that hibernate would take care of getting the autoincrement value?
Business Accounts
Answer for Membership
by: CEHJPosted on 2009-07-16 at 11:55:04ID: 24872425
You're already doing it - with getGeneratedKeys..?