• 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

getting ID after Insert.

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all.
Does somebody know, how to solve this problem:
In my code I insert a new record into the table.
...
String sqlStmt = "INSERT INTO Texts (text) VALUES (?)";
PreparedStatement stmt = getConnection().prepareStatement(sqlStmt);
stmt.setString(1,newtext); //newtext is String.
stmt.executeUpdate();
...
And right after(immediatly) I need to get text_ID (that is given by DB Server) number
of this new created Text-record. I don't use 'SELECT text_ID from Where text = newtext',
because this value (the column 'text') maybe more than one.
They maybe works more users at the same time,
therefore I cann't use 'sort by text_ID' and get the last record.
Table looks like:
Texts{
text_id // column is PrimKey and automatic from DB Server
text // 'normal' column. Some string value. Maybe duplicate.
}
Thanks for your idea, answer.
 
Ranch Hand
Posts: 276
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This has been answered a number of times.
Do a search on jdbc for identity (sql svr), sequence (oracle), etc
basically you can do the insert in a stored procedure and return the key
Dan
 
Ranch Hand
Posts: 1879
MySQL Database Suse
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
if it is possible for you to use the jdk1.4SE then you could consider this:
http://java.sun.com/j2se/1.4/docs/api/java/sql/Statement.html#executeUpdate(java.lang.String,%20int)
followed by
http://java.sun.com/j2se/1.4/docs/api/java/sql/Statement.html#getGeneratedKeys()
Otherwise your stuck with the above posts.
Jamie
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic