Solved

getGeneratedKey() only returns last record of inserted batch

Posted on 2003-11-12
6
1,063 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

 
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

Industry Leaders: 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!

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

695 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