• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 @");
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Just so you know!
Please read this before using LAST_INSERT_ID
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic