Originally posted by Tay Thotheolh:
Hi. I have an 'ID' column for my database (MySQL) which is set to auto_increment. Everytime I add a row, the 'id' auto_increments. How do I get the auto_incremented 'ID' value ?
I doubt if I get the most recently added row would work because what happen if there maybe a chance someone did an insert after you very quickly ?
Cheers, Martijn,
Twitter.
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
I doubt if I get the most recently added row would work because what happen if there maybe a chance someone did an insert after you very quickly ?
Originally posted by Tay Thotheolh:
I am trying to get the latest id to do another insert into another table.
Originally posted by Tay Thotheolh:
Maybe I should try the getGeneratedKeys().
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
Originally posted by Campbell Ritchie:
Isn't there a MySQL function called last_insert_id()? I think that is what you are looking for. Try here and use ctrl-F "last".
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
Originally posted by Scott Selikoff:
[last_insert_id()]
Doesn't work well in multi-threaded environment because I believe you could get the value someone else inserted.
Originally posted by Scott Selikoff:
What can I say, this post inspired me to write an article on the subject: Database Key Generation in Java Applications
[ September 03, 2008: Message edited by: Scott Selikoff ]
Cheers, Martijn,
Twitter.
No.Originally posted by dee:
Hi,
I was just wondering if getGeneratedKeys is safe to use in multi-threaded environment. If you insert a row into a table and then call the getGeneratedKeys method immediately (as in Scott's example at http://www.selikoff.net/blog/2008/09/03/database-key-generation-in-java-applications/), does this count as one transaction?
Thanks,
Dee
Originally posted by Campbell Ritchie:
No.
Not unless you have START TRANSACTION; before it and COMMIT; afterwards.
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
Originally posted by Martijn Verburg:
Good stuff Scott, I've got a group of Grads devouring it as I type
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
Originally posted by Scott Selikoff:
Awesome, get them hooked on The Daily WTF at the same time, it'll give them perspective.
Cheers, Martijn,
Twitter.
Originally posted by Martijn Verburg:
Already done , they already hate the cartoons and were suitably shocked at how unprofessional the real world is, you're totally right about setting them straight on that as early as possible.
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
Originally posted by Campbell Ritchie:
I appear to be mistaken; sorry.
[OCP 21 Professional Study Guide] [OCP 17 Complete Study Guide] [OCP 17 Practice Tests] [OCP 11 Complete Study Guide] [OCP 11 Programmer I] [OCP 11 Programmer II] [OCP 11 Practice Tests] [OCA 8] [OCP 8] [OCP 8 Practice Tests Book] [Blog]
I miss the old days when I would think up a sinister scheme for world domination and you would show a little emotional support. So just look at this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
|