• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1068
  • Last Modified:

getGeneratedKey() only returns last record of inserted batch

I have a batch that inserts more than one record at a time.  After the batch call I call getGeneratedKeys on the preparedStatement, but it only returns the last record inserted (MySQL database)...

//sample code

/* code to delare preparedStatement and code to set values and add to batch */

int[] updateCounts = object.executeBatch();
ResultSet rs = object.getGeneratedKeys ();
0
sapientconceptions
Asked:
sapientconceptions
  • 3
  • 2
1 Solution
 
objectsCommented:
Don't think you can use getGeneratedKeys() when using batch processing.
 
0
 
sapientconceptionsAuthor Commented:
ok, but I thought that was the whole reason for the method (especially since it's named with an 's' on the end)...

I guess I'll have to create my own id's and send with the insert, but then the question becomes -- how can I easily get the last id in the database?
0
 
objectsCommented:
> but I thought that was the whole reason for the method
> (especially since it's named with an 's' on the end)...

For statements that generate multiple id's.

> I  guess I'll have to create my own id's and send with the insert

Or don't use a batch and execute the insert's individually.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
jimmackCommented:
objects actually answered your last question a couple of months ago ;-)

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20733689.html
0
 
sapientconceptionsAuthor Commented:
Objects,

Sometimes you just need someone else to tell you the obvious!  Thanks for the help.
0
 
objectsCommented:
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now