Results 1 to 4 of 4
  1. #1
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default how to SELECT LAST_INSERT_ID() in java

    I need to find out an auto_incrementing amount using LAST_INSERT_ID(). Is there a specific way to get that query executed and returned?

    Java Code:
    Resultset resultS = statement.executeQuery("SELECT LAST_INSERT_ID()");
    When I use this, I get this value returned.

    com.mysql.jdbc.JDBC4ResultSet@119298d

    Am I right in assuming that this "119298d" is the value created by AUTO_INCREMENT? If so, how do I seperate it once in the resultset? (next())?

    Also, if I want to use that value to create a unique set of results for a group, what type would it be (re)stored in? I'm guessing not an int. varchar(10)?!?

    Sorry for all the questions, but this is important I get this done.

    Thanks in advance.

  2. #2
    int80 is offline Member
    Join Date
    Jul 2008
    Posts
    29
    Rep Power
    0

    Default

    I've just realised, it can't be the value returned as, when I SELECT * FROM group_order; I get this:

    Java Code:
    +---------+-------------+
    | groupID | bookingDate |
    +---------+-------------+
    |       1 | 12/12/2008  |
    |       2 | 12/12/2008  |
    |       3 | 12/12/2008  |
    |       4 | 12/12/2008  |
    |       5 | 12/12/2008  |
    |       6 | 12/12/2008  |
    |       7 | 12/12/2008  |
    |       8 | 01/12/2000  |
    +---------+-------------+
    groupID is incrementing from 1, so god knows where that alphanumeric number came from.

  3. #3
    kalikakiran is offline Member
    Join Date
    Aug 2008
    Posts
    1
    Rep Power
    0

    Smile Hai Everybody

    Hai This Is My First Message To Everybody:):

  4. #4
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by int80 View Post
    I need to find out an auto_incrementing amount using LAST_INSERT_ID(). Is there a specific way to get that query executed and returned?

    com.mysql.jdbc.JDBC4ResultSet@119298d

    Am I right in assuming that this "119298d" is the value created by AUTO_INCREMENT?
    First, "119298d" is the address of the result set, not a value within it. it is not at all the value you are looking for.

    You should test this. Start with a table with one record, do the insert, and if you don't get "2" or something close, you have it wrong.

    You need to explicitly use the JDBC functions to return it. You start with something like:

    Java Code:
          int numRows = stmt.executeUpdate(command,      Statement.RETURN_GENERATED_KEYS);
    which does the insert and tells JDBC that you want the generated key. Then you have to get a new result set for it:
    Java Code:
     ResultSet rs = null;
                    try {
                        rs = stmt.getGeneratedKeys();
                        if (rs.next()) {
                            autoIncKeyFromApi = rs.getInt(1);
                        } else {
                            throw new RuntimeException("PIB, can't find most recent insert we just entered");
                        }
                        rs.close();
                        rs = null;
                    }  finally {
                        if (rs != null) {
                            try {
                                rs.close();
                            } catch (SQLException ex) {} // ignore
                        }
                        try {
                            stmt.close();
                        } catch (SQLException ex) {}  // ignore
                    }

Similar Threads

  1. SELECT FROM WHERE query
    By herfnai in forum JDBC
    Replies: 3
    Last Post: 11-24-2008, 11:04 AM
  2. Select Count
    By Apple2 in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 04-29-2008, 09:02 AM
  3. Using a variable in a SELECT FROM WHERE query
    By cplmckenzie in forum JDBC
    Replies: 12
    Last Post: 04-23-2008, 03:24 AM
  4. Displaying a Java List in Html:select tag
    By ramitmehra123 in forum New To Java
    Replies: 0
    Last Post: 02-07-2008, 05:48 AM
  5. Select specific cell
    By Echilon in forum New To Java
    Replies: 1
    Last Post: 01-01-2008, 07:47 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •