Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trouble using LAST_INSERT_ID() to retrieve the last inserted record ID

 
Brian K Swingle
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,
I'm using the code below to write data about a particular user to a database table then i want to retrieve the Unique Key for purposes of passing the number on to via email so that support personel may retirieve the users data at a later time. I'm using MySQL and a JDBC connection obvously. The issue i'm having is not that i'm recieveing an error. But the LAST_INSERT_ID() only returns '0' . and the RecordNumber field which is the Unique Key field and is set to Auto Increment is up to like 12,000 rows. Do you have any idea why it would return '0' Am i using this correctly? Any help would be greatly appreciated.
Thanks
Brian
conn2 = DriverManager.getConnection("jdbc:mysql://localhost/Database?user=User&password=Password");
stmt2 = conn2.createStatement();
String dbquery = "INSERT INTO Dump ( ) VALUES ( )";
rs3 = stmt.executeQuery(dbquery);
System.out.println("@ Before Get Row ID@");
rs3 = stmt2.executeQuery("select LAST_INSERT_ID()");
System.out.println("@ Query Completed Get Row ID @");
if (rs3.next())
{
NewRecNum = rs3.getInt(1);
System.out.println("@NewRecNum: '"+NewRecNum+"' @");
}
System.out.println("@ After Get Row ID @");
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just so you know!
Please read this before using LAST_INSERT_ID
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic