Solved

getGeneratedKey() only returns last record of inserted batch

Posted on 2003-11-12
6
1,047 Views
Last Modified: 2007-12-19
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
Comment
Question by:sapientconceptions
  • 3
  • 2
6 Comments
 
LVL 92

Expert Comment

by:objects
ID: 9737650
Don't think you can use getGeneratedKeys() when using batch processing.
 
0
 

Author Comment

by:sapientconceptions
ID: 9737777
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
 
LVL 92

Accepted Solution

by:
objects earned 250 total points
ID: 9737808
> 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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 
LVL 15

Expert Comment

by:jimmack
ID: 9738046
objects actually answered your last question a couple of months ago ;-)

http://www.experts-exchange.com/Programming/Programming_Languages/Java/Q_20733689.html
0
 

Author Comment

by:sapientconceptions
ID: 9742203
Objects,

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

Expert Comment

by:objects
ID: 9742252
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now