locked
Getting generated GUID using jdbc RRS feed

  • Question

  • I'm using SQL Server 2005 and have a table with a primary key that is generated using NewSequentialID().  I've set my Statement object in JDBC to return generated keys and I can see that after calling the Statement I do have a ResultSet.  My problem is that I can't get the generated value.  I've attempted doing this by using the column index (1), getting the ResultSetMetaData and trying to get the column name (returns as empty string), and trying various "made-up" values for the column name.

    Looking at the ResultSetMetaData, it appears that the column type being returned is "numeric" (this is using resultsetmetadata.getColumnTypeName(1)).  This is clearly not what is expected.  Is this a limitation of the driver?  I've just downloaded the most recent version of the driver (from early 2010 apparently).  

     

    Thanks for any help

    -Chris

    Tuesday, July 26, 2011 10:01 PM

Answers

  • Hi Chris,

    You can get the last inserted ID which is using auto_generated primary key by getGeneratedKeys() method  of java.sql.PreparedStatement. For more information: JDBC: Get Last Insert's ID.

    As to the value of GUID, it is not generated incrementally. You might need to use some workarounds in the database side while performing the DML operation to get the GUID value.  Since you are using SQL Server 2005, so it available to utilize the OUTPUT clause with DML statements to return the GUID.  Please take a look at this thread on this topic: Get the last inserted uniqueidentifier field.

    Hope this helps.


    Best Regards,
    Stephanie Lv

    Forum Support
    Please remember to mark the replies as answers if they help and unmark them if they provide no help. If you have feedback for TechNet Subscriber Support, contact tnmff@microsoft.com.
    Thursday, July 28, 2011 6:00 AM