Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 08-26-2008, 02:31 AM
Member
 
Join Date: Jul 2008
Posts: 27
int80 is on a distinguished road
how to SELECT LAST_INSERT_ID() in java
I need to find out an auto_incrementing amount using LAST_INSERT_ID(). Is there a specific way to get that query executed and returned?

Code:
Resultset resultS = statement.executeQuery("SELECT LAST_INSERT_ID()");
When I use this, I get this value returned.

com.mysql.jdbc.JDBC4ResultSet@119298d

Am I right in assuming that this "119298d" is the value created by AUTO_INCREMENT? If so, how do I seperate it once in the resultset? (next())?

Also, if I want to use that value to create a unique set of results for a group, what type would it be (re)stored in? I'm guessing not an int. varchar(10)?!?

Sorry for all the questions, but this is important I get this done.

Thanks in advance.
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-26-2008, 03:00 AM
Member
 
Join Date: Jul 2008
Posts: 27
int80 is on a distinguished road
I've just realised, it can't be the value returned as, when I SELECT * FROM group_order; I get this:

Code:
+---------+-------------+ | groupID | bookingDate | +---------+-------------+ | 1 | 12/12/2008 | | 2 | 12/12/2008 | | 3 | 12/12/2008 | | 4 | 12/12/2008 | | 5 | 12/12/2008 | | 6 | 12/12/2008 | | 7 | 12/12/2008 | | 8 | 01/12/2000 | +---------+-------------+
groupID is incrementing from 1, so god knows where that alphanumeric number came from.
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 08-26-2008, 03:23 AM
Member
 
Join Date: Aug 2008
Posts: 1
kalikakiran is on a distinguished road
Hai Everybody
Hai This Is My First Message To Everybody:
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 08-26-2008, 06:43 AM
fishtoprecords's Avatar
Senior Member
 
Join Date: Jun 2008
Posts: 459
fishtoprecords is on a distinguished road
Quote:
Originally Posted by int80 View Post
I need to find out an auto_incrementing amount using LAST_INSERT_ID(). Is there a specific way to get that query executed and returned?

com.mysql.jdbc.JDBC4ResultSet@119298d

Am I right in assuming that this "119298d" is the value created by AUTO_INCREMENT?
First, "119298d" is the address of the result set, not a value within it. it is not at all the value you are looking for.

You should test this. Start with a table with one record, do the insert, and if you don't get "2" or something close, you have it wrong.

You need to explicitly use the JDBC functions to return it. You start with something like:

Code:
int numRows = stmt.executeUpdate(command, Statement.RETURN_GENERATED_KEYS);
which does the insert and tells JDBC that you want the generated key. Then you have to get a new result set for it:
Code:
ResultSet rs = null; try { rs = stmt.getGeneratedKeys(); if (rs.next()) { autoIncKeyFromApi = rs.getInt(1); } else { throw new RuntimeException("PIB, can't find most recent insert we just entered"); } rs.close(); rs = null; } finally { if (rs != null) { try { rs.close(); } catch (SQLException ex) {} // ignore } try { stmt.close(); } catch (SQLException ex) {} // ignore }
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
SELECT FROM WHERE query herfnai Database 1 Yesterday 06:53 AM
Select Count Apple2 JavaServer Pages (JSP) and JSTL 1 04-29-2008 11:02 AM
Using a variable in a SELECT FROM WHERE query cplmckenzie Database 12 04-23-2008 05:24 AM
Displaying a Java List in Html:select tag ramitmehra123 New To Java 0 02-07-2008 07:48 AM
Select specific cell Echilon New To Java 1 01-01-2008 09:47 AM


All times are GMT +3. The time now is 02:26 AM.


VBulletin, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2007, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org