[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Getting the last inserted ID from table mySQL and java

Posted on 2006-03-21
5
Medium Priority
?
4,911 Views
Last Modified: 2012-08-13
Would like to get the last inserted ID from mySQL table and display it on a text box. The following is my code:
// RETRIEVING GENERATED WO_NUMBER FROM A NEW DATA RECORD (AUTO_INCREMENT)
public int get_woID() throws Exception{
 __int woID=0;
      try{
          Statement stmt = con.createStatement();
          //ResultSet result = stmt.executeQuery("SELECT LAST_INSERT_ID(id) from table1");
____stmt.executeQuery("");
____ResultSet newid = stmt.getGeneratedKeys();
____newid.next();
____woID = newid.getInt(1);
__     }//try
     catch(SQLException sqle){
       System.out.println("Error Executing SQL");
       throw(sqle);
     }//catch
   return woID;
}

please help me get the correct values
0
Comment
Question by:mphagob
3 Comments
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 120 total points
ID: 16246332
the query to run is very short:

SELECT LAST_INSERT_ID()
0
 
LVL 6

Assisted Solution

by:avinthm
avinthm earned 120 total points
ID: 16246334

> Statement stmt = con.createStatement();
>           //ResultSet result = stmt.executeQuery("SELECT LAST_INSERT_ID(id)
> from table1");

 Statement stmt = con.createStatement();
 ResultSet result = stmt.executeQuery("SELECT max(id) from table1");
 int lastInsertedId = result.getInt("id");

0
 
LVL 3

Assisted Solution

by:int_20h
int_20h earned 120 total points
ID: 16250265
Also, for compatibility to others DBs, you can use:

SELECT @@IDENTITY

I know it works on mySql 5.0, so, hope if works for your version.  This variable is a synonym for the LAST_INSERT_ID variable.
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
Computer science students often experience many of the same frustrations when going through their engineering courses. This article presents seven tips I found useful when completing a bachelors and masters degree in computing which I believe may he…
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 …
Screencast - Getting to Know the Pipeline

865 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