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
Solved

getGeneratedKey() only returns last record of inserted batch

Posted on 2003-11-12
6
1,056 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
Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
How to parse custom JSON to POJO java 4 83
hibernate insert example 13 34
Java syntax, or is it Selenium 6 30
Java Eclipse Loop 3 20
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

809 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