Solved

getGeneratedKey() only returns last record of inserted batch

Posted on 2003-11-12
6
1,051 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Protect jar file - windows app 2 38
best (free) software to access postgres db (java) 1 32
Java Timer (static) 9 36
custom annotations 9 28
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…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

932 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

11 Experts available now in Live!

Get 1:1 Help Now