If you are using the Mark Matthews JDBC driver (MM.MySQL), there's another way to get 
the last insert id:  instead of using a generic Statement object to execute the INSERT 
command, use an org.gjt.mm.mysql.Statement object.  After executing an INSERT, the 
method getLastInsertID() will return the value of LAST_INSERT_ID.  This value is sent 
to the client along with other status info after the INSERT command is executed,  so 
the method does not require another call to the server.

--Greg Johnson

                -----Original Message-----
                From:   Carl Troein [mailto:[EMAIL PROTECTED]]
                Sent:   Friday, November 09, 2001 10:41 AM
                To:     [EMAIL PROTECTED]
                Subject:        Re: Beginner question - getting last inserted ID


                Anna Åhnberg writes:

                > Thanks, I actually already found the chapters but now I also now how 
to
                > use the function too!

                Let me quote from the manual:
                "LAST_INSERT_ID([expr]) 
                     Returns the last automatically generated value that was
                     inserted into an AUTO_INCREMENT column.
                mysql> select LAST_INSERT_ID();
                             -> 195"

                Thus you'd follow these steps:
                1) INSERT into the table, and leave out the auto column or supply
                   a value of 0 or NULL
                2) Verify that the query succeeded
                3) SELECT LAST_INSERT_ID()
                4) Get the result of the SELECT. On success, the SELECT will
                   return one row with one value in it. That is the number you're
                   interested in knowing.

                If you were using MySQL's C API there is a function that returns
                the ID without the need for steps 3-4, but since you are
                communicating over JDBC I don't think there's a simpler solution
                available. The query in step 3 is extremely fast, so it doesn't
                really matter, except that it's a bit more work to make an extra
                query.

                //C

                -- 
                 Carl Troein - Círdan / Istari-PixelMagic - UIN 16353280
                 [EMAIL PROTECTED] | http://pixelmagic.dyndns.org/~cirdan/
                 Amiga user since '89, and damned proud of it too.


                ---------------------------------------------------------------------
                Before posting, please check:
                   http://www.mysql.com/manual.php   (the manual)
                   http://lists.mysql.com/           (the list archive)

                To request this thread, e-mail <[EMAIL PROTECTED]>
                To unsubscribe, e-mail 
<[EMAIL PROTECTED]>
                Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

---------------------------------------------------------------------
Before posting, please check:
   http://www.mysql.com/manual.php   (the manual)
   http://lists.mysql.com/           (the list archive)

To request this thread, e-mail <[EMAIL PROTECTED]>
To unsubscribe, e-mail <[EMAIL PROTECTED]>
Trouble unsubscribing? Try: http://lists.mysql.com/php/unsubscribe.php

Reply via email to