Results 1 to 6 of 6
  1. #1
    JustinCase is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default retrieving PK after insertRow()

    I am doing a simple db operation using a resultset. Below is my code for an INSERT:

    Java Code:
    stmt = connection.createStatement( ResultSet.TYPE_SCROLL_SENSITIVE, ResultSet.CONCUR_UPDATABLE);
    rs = stmt.executeQuery("SELECT * FROM table LIMIT 0");
    rs.moveToInsertRow();
    rs.updateString("name", "test");
    rs.updateString("description", "desc");
    rs.updateInt("customerid", iCustomerID);
    rs.insertRow();
    Now i want to return the PK ID that was created in the database. But rs.getInt("PrimaryKeyRow") always returns 0, although i see in my DB that the row has got an ID. How can I get the generated ID? And I do NOT want to use a method that does not let me edit a resultset (meaning I do not want to build a query and send it to rs.execute(sqlStatement, Statement.RETURN_GENERATED_KEYS), as I want to loop through the set and make updates and inserts depending on the existing rows).

  2. #2
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    See the API docs for statement and the varying execute methods to discover a way to inidicate that autgeneratedkeys should be "remembered" (and possibly defining which columns those entail) and a method call with which you can then retreive those keys after calling the execute method of your wish.

  3. #3
    JustinCase is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Yes

    I am aware of this, but as I said, I do not want to use methods that require me to create an update statement and pass it to the server, and then get the ID's back. I want to get a resultset that i can browse, and then make updates and inserts into it.

  4. #4
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Then good luck with that. If you don't want to use the tools available you will need to find a way to create your own.

  5. #5
    JustinCase is offline Member
    Join Date
    May 2011
    Posts
    5
    Rep Power
    0

    Default Ok...?

    So there is no way to do this? Seems like it should be a pretty standard thing. With a resultset, i can do dynamic updates and inserts without refetching the data. Also the code above for updating is a lot smoother than building a text query string for an update, among other things it removes the need for '' around text parameters.

    Anyone else have any ideas here? Like I said, seems to me like it should be the obvious way to do this.

  6. #6
    masijade is offline Senior Member
    Join Date
    Jun 2008
    Posts
    2,571
    Rep Power
    9

    Default

    Quote Originally Posted by JustinCase View Post
    So there is no way to do this? Seems like it should be a pretty standard thing. With a resultset, i can do dynamic updates and inserts without refetching the data. Also the code above for updating is a lot smoother than building a text query string for an update, among other things it removes the need for '' around text parameters.
    There is no need for those anyway. That's one of the things PreparedStatement is for

    Anyone else have any ideas here? Like I said, seems to me like it should be the obvious way to do this.
    Well, seeing as the API provides no method declarations for this I would say that the developers did not consider this to be the "obvious" way. You could, obviously, simply try doing getWhatever on the normally generated columns directly after doing insertRow, not that I think it would accomplish anything.

Similar Threads

  1. retrieving percentages
    By droidus in forum New To Java
    Replies: 16
    Last Post: 04-20-2011, 02:26 PM
  2. Retrieving a parameter through URL.
    By hisouka in forum Java Servlet
    Replies: 1
    Last Post: 09-06-2008, 12:45 PM
  3. retrieving Properties
    By bugger in forum New To Java
    Replies: 1
    Last Post: 01-13-2008, 09:44 PM
  4. Retrieving out objects from Vector
    By tuckker in forum New To Java
    Replies: 3
    Last Post: 12-03-2007, 05:52 AM
  5. Retrieving data from the DB
    By yuchuang in forum JDBC
    Replies: 2
    Last Post: 11-27-2007, 08:59 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
  •