Unix Technical Forum

need auto increment value

This is a discussion on need auto increment value within the MySQL General forum forums, part of the MySQL category; --> Hello Everybody, i want to read the latest value of a autoincrement column from a java program. How can ...


Go Back   Unix Technical Forum > Database Server Software > MySQL > MySQL General forum

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-27-2008, 09:31 PM
balaraju mandala
 
Posts: n/a
Default need auto increment value

Hello Everybody,

i want to read the latest value of a autoincrement column from a java
program. How can i do this? i want to do this in a single query insertion,
is it possible?

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-27-2008, 09:31 PM
=?ISO-8859-1?Q?Johan_H=F6=F6k?=
 
Posts: n/a
Default Re: need auto increment value

Hello,
you can do it in two ways I guess:
one is to do a second select (which you don't want):
SELECT LAST_INSERT_ID().

another way is to use java.sql.Statement.RETURN_GENERATED_KEYS
when you create your Statement, something along these lines:

java.sql.PreparedStatement pstmt =
myconnection.prepareStatement("INSERT INTO
mytable(value)VALUES('value')",
java.sql.Statement.RETURN_GENERATED_KEYS );
pstmt.executeUpdate();
java.sql.ResultSet rs = pstmt.getGeneratedKeys();
while( rs.next())
System.out.println("My autoincrementid is: " + rs.getInt(1));

/Johan

balaraju mandala skrev:
> Hello Everybody,
>
> i want to read the latest value of a autoincrement column from a java
> program. How can i do this? i want to do this in a single query insertion,
> is it possible?
>
>
> ------------------------------------------------------------------------
>
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.1.408 / Virus Database: 268.13.2/471 - Release Date: 2006-10-10


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
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
Forum Jump


All times are GMT. The time now is 12:20 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com