List:General Discussion« Previous MessageNext Message »
From:Johan Höök Date:October 11 2006 8:59am
Subject:Re: need auto increment value
View as plain text  
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

Thread
need auto increment valuebalaraju mandala11 Oct
  • Re: need auto increment valueJohan Höök11 Oct