dbTalk Databases Forums  

how to find the last insert id ?

mailing.database.mysql-java mailing.database.mysql-java


Discuss how to find the last insert id ? in the mailing.database.mysql-java forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Guillaume MOCCAND
 
Posts: n/a

Default how to find the last insert id ? - 05-27-2004 , 05:02 PM







Hi !

i wonder how i can get the ID of the last insert made from the MySQL
Connector/J driver ?
does any one has an exemple of this ?

thank's


Guillaume MOCCAND
moccand_g (AT) hotmail (DOT) com

__________________________________________________ _______________
MSN Actions Solidaires : http://www.msn.fr/actionssolidaires/ la
solidarité à portée de click


--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/java?unsub=my...ie.nctu.edu.tw


Reply With Quote
  #2  
Old   
Ben Dinnerville
 
Posts: n/a

Default RE: how to find the last insert id ? - 05-27-2004 , 06:27 PM






You can use the JDBC 3.0 getGenerated() keys call as follows:

Int generatedKey = -1;
ResultSet rs = stat.getGeneratedKeys();
if (rs.next()){
generatedKey = rs.getInt(1);
}
rs.close();

Chhers,

Ben

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.691 / Virus Database: 452 - Release Date: 26/05/2004



--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/java?unsub=my...ie.nctu.edu.tw


Reply With Quote
  #3  
Old   
Irfandhy Franciscus
 
Posts: n/a

Default Re: how to find the last insert id ? - 05-29-2004 , 12:50 AM



Perhaps you can try something like this:

SELECT MAX(tbl.ID) FROM [table_name] tbl ?



Guillaume MOCCAND wrote:
Quote:
Hi !

i wonder how i can get the ID of the last insert made from the MySQL
Connector/J driver ?
does any one has an exemple of this ?

thank's


Guillaume MOCCAND
moccand_g (AT) hotmail (DOT) com

__________________________________________________ _______________
MSN Actions Solidaires : http://www.msn.fr/actionssolidaires/ la
solidarité à portée de click



--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/java?unsub=my...ie.nctu.edu.tw



Reply With Quote
  #4  
Old   
Daniel Clark
 
Posts: n/a

Default Re: how to find the last insert id ? - 05-29-2004 , 09:46 AM



How about: http://forum.java.sun.com/thread.jsp...essage=2229028

Last_INSERTED_ID() method


Quote:
Perhaps you can try something like this:

SELECT MAX(tbl.ID) FROM [table_name] tbl ?



Guillaume MOCCAND wrote:

Hi !

i wonder how i can get the ID of the last insert made from the MySQL
Connector/J driver ?
does any one has an exemple of this ?

thank's



--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/java?unsub=my...ie.nctu.edu.tw



Reply With Quote
  #5  
Old   
Ben Dinnerville
 
Posts: n/a

Default RE: how to find the last insert id ? - 05-30-2004 , 06:40 PM



" Can someone explain to me why there is no support for retrieving this
value using the JDBC API "

Does nobody read here?? The very first reply to this thread (by me) =
showed
you how you could retrieve this data via a standard JDBC call, since =
then,
other hacks have been floating around - for what reason I don=92t know, =
as the
JDBC 3.0 method works fine.

Just to recap, for those that didn=92t read the first post, you can use =
the
JDBC 3.0 (MySQL is has a JDBC 3 compliant driver) method as such:

Statement stat =3D dbConn.createStatement();
Stat.executeUpdate("Some insert statement here");

ResultSet rs =3D stat.getGeneratedKeys();
Int insertedKeyValue =3D rs.getInt(1);

Rs.close();
Stat.close();


Cheers

Ben=20

---
Outgoing mail is certified Virus Free.
Checked by AVG anti-virus system (http://www.grisoft.com).
Version: 6.0.692 / Virus Database: 453 - Release Date: 28/05/2004
=20


--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/java?unsub=3D...ie.nctu.edu.tw


Reply With Quote
  #6  
Old   
Rikard Froberg
 
Posts: n/a

Default Re: how to find the last insert id ? - 06-01-2004 , 05:30 AM



Guillaume MOCCAND wrote:
Quote:
Hi !

i wonder how i can get the ID of the last insert made from the MySQL
Connector/J driver ?
does any one has an exemple of this ?
Hi, Guillaume,

I don't know if this is the prefered way of doing it, but this is how
we do it:

rs = stmt.executeQuery("SELECT LAST_INSERT_ID()");
rs.next();
id = rs.getString(1);

It returns the last instert id of the session connected to
stmt, so you'll have to look out for concurrency and pooling
issues...

Rikard
Quote:
thank's


Guillaume MOCCAND
moccand_g (AT) hotmail (DOT) com

__________________________________________________ _______________
MSN Actions Solidaires : http://www.msn.fr/actionssolidaires/ la
solidarité à portée de click


--
MySQL Java Mailing List
For list archives: http://lists.mysql.com/java
To unsubscribe: http://lists.mysql.com/java?unsub=my...ie.nctu.edu.tw



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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.