Welcome to dbForumz.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

Getting the id of a newly added record

 
   Database Forums (Home) -> Java RSS
Next:  FMP10: copy/paste from status bar, instant resort  
Author Message
james

External


Since: Feb 09, 2009
Posts: 1



(Msg. 1) Posted: Mon Feb 09, 2009 2:52 am
Post subject: Getting the id of a newly added record
Archived from groups: comp>lang>java>databases (more info?)

Hi,

I have a MySQL database table, with a few fields, and the first field
is id, which is set to autoincrement. After adding an item to the
table, I need to know the id of the newly added item, so that I can
edit it afterwards. Is there any way to find the id of the item,
using either SQL or the Java MySQL library?

Just finding the item most recently added to the table would be risky,
because the table is going to be edited a lot, and by multiple
applications concurrently.

Thanks,

--James.

 >> Stay informed about: Getting the id of a newly added record 
Back to top
Login to vote
Martin Gregorie

External


Since: Feb 09, 2009
Posts: 1



(Msg. 2) Posted: Mon Feb 09, 2009 7:34 am
Post subject: Re: Getting the id of a newly added record [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

On Mon, 09 Feb 2009 02:52:01 -0800, james wrote:

> I have a MySQL database table, with a few fields, and the first field is
> id, which is set to autoincrement. After adding an item to the table, I
> need to know the id of the newly added item, so that I can edit it
> afterwards. Is there any way to find the id of the item, using either
> SQL or the Java MySQL library?
>
> Just finding the item most recently added to the table would be risky,
> because the table is going to be edited a lot, and by multiple
> applications concurrently.
>
run SELECT LAST_INSERT_ID(); immediately after the INSERT.

The manual says this is connection-specific, so not affected by activity
on other conections.

This isn't a Java question, so kindly RTFM in future before asking
database specific questions. It took me about 10 seconds to find this
answer in the manual - and I've never, ever used MySQL.


--
martin@ | Martin Gregorie
gregorie. | Essex, UK
org |

 >> Stay informed about: Getting the id of a newly added record 
Back to top
Login to vote
Chris Riesbeck

External


Since: Feb 09, 2009
Posts: 1



(Msg. 3) Posted: Mon Feb 09, 2009 1:39 pm
Post subject: Re: Getting the id of a newly added record [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Martin Gregorie wrote:
> On Mon, 09 Feb 2009 02:52:01 -0800, james wrote:
>
>> I have a MySQL database table, with a few fields, and the first field is
>> id, which is set to autoincrement. After adding an item to the table, I
>> need to know the id of the newly added item, so that I can edit it
>> afterwards. Is there any way to find the id of the item, using either
>> SQL or the Java MySQL library?
>>
>> Just finding the item most recently added to the table would be risky,
>> because the table is going to be edited a lot, and by multiple
>> applications concurrently.
>>
> run SELECT LAST_INSERT_ID(); immediately after the INSERT.
>
> The manual says this is connection-specific, so not affected by activity
> on other conections.
>
> This isn't a Java question, so kindly RTFM in future before asking
> database specific questions. It took me about 10 seconds to find this
> answer in the manual - and I've never, ever used MySQL.
>

There's a JDBC answer as well. Look up generated keys in Statement and
PreparedStatement.
 >> Stay informed about: Getting the id of a newly added record 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
record numbers - Maybe my question is stupid and wrong but I hope that you will understand. So, image that you need to create web application which will used by many peoples in one LAN. They processing some documents and every person has the particular number of..

Access Database update with pstmt.executeUpdate(), no reco.. - I testing pstmt.executeUpdate();, found that the Access does not update. What wrong ? Where cnt is integer,pt_part is String. C:\Example\javaux\jdbc>java access_update Update pt_mstr set cnt = ? where pt_part = ? C:\Example\javaux\jdbc> //* ....

2nd Globals Programming Challenge - USD $3,500 Grand Prize.. - Want to show how good you are at coding "Big Data" solutions? InterSystems Corp. is hosting a series of programming challenges in the Globals Community. Our 2nd Globals Challenge kicks off with a two day competition on Friday, December 02, 20...

algorithm for generating top fuzzy variations ... - Hello all, I am interested in obtaining the top N fuzzy variations of an string (a person or company name) using the same concept as the Levenshtein distance. Ussually Levenshtein is used to compute the distance between two given strings ... but I would...

Bug in Oracle org.w3c.dom.Node ? - Hello, I am using Oracle 10g for retrieving XML documents as XMLType and manipulate them in Java, using the method org.w3c.dom.Node.setTextContent() to set the text child node of a DOM node. The oracle DOM implementation just appends a child node to the...
   Database Forums (Home) -> Java All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]