PreparedStatement and getGeneratedKeys for newSequentialId()

Unanswered PreparedStatement and getGeneratedKeys for newSequentialId()

  • Wednesday, January 16, 2013 7:28 PM
     
     


    Environment:   I am using MSSQL Server 2008. My JDBC driver jar is sqljdbc4.jar.

    Problem:

    My program inserts a row every time but fails to get the generated key. My insert data is dynamic and I should be able to use PreparedStatement to do this.

    Here is my schema

    CREATE TABLE TABLE_A(

    [ID] [uniqueidentifier] DEFAULT NEWSEQUENTIALID() NOT NULL,
    [name] varchar(30) not null
    )

    My test program is as follows:

       

    public class GeneratedKeys {

    //To DB
    private String toDBIP = "localhost";
    private String toDBUserName = "****";
    private String toDBPassword = "****";
    private String toDBSchemaName = "****";

    private static Connection getConnection(String DBIP, String DBUserName, String DBPassword, String DBSchemaName) {
    Connection con = null;
    try {
           Properties prop = new Properties();
           prop.put("user", DBUserName);
           prop.put("password", DBPassword);
           prop.put("databaseName", DBSchemaName);
           con = DriverManager.getConnection("jdbc:sqlserver://" +
                                             DBIP + ":" +
                                             "1433", prop);
    }catch (Exception ex) {
    System.out.println("Exception in getConnection:");
    ex.printStackTrace();
    }
    return con;
    }

    public void insertData(Connection con) {
    try {
    con.setAutoCommit(false);
    PreparedStatement insertStmt = con.prepareStatement("insert into table_A (name) values(?)", Statement.RETURN_GENERATED_KEYS);
    insertStmt.setString(1, "ABC");
    int result = insertStmt.executeUpdate();
    con.commit();
    ResultSet rs = insertStmt.getGeneratedKeys();
    if((result>0) && (rs!=null) && rs.next()) {
    System.out.println("Generated key -- " + rs.getString(1));
    }
    }catch(SQLException sqle) {
    sqle.printStackTrace();
    }
    }

    public static void main(String args[]) {
    GeneratedKeys obj = new GeneratedKeys();
    Connection toDB = GeneratedKeys.getConnection(obj.toDBIP, obj.toDBUserName, obj.toDBPassword, obj.toDBSchemaName);
    obj.insertData(toDB);
    try {
    toDB.close();
    } catch (SQLException e) {
    System.out.println("Exception while closing connection:");
    e.printStackTrace();
    }
    }

    }

    Program generated output:

    Generated key -- null

    select * from TABLE_A;

    ID name
    E159D82B-1260-E211-BB7E-005056C00008 ABC



    Update: 
    if((result>0) && (rs!=null) && rs.next()) {
    System.out.println("Generated key -- " + rs.getRowId(1));
    }

    gives

    java.sql.SQLFeatureNotSupportedException: This operation is not supported.

    Is this going to be available soon ?

    • Edited by SasiVatsa Wednesday, January 16, 2013 8:15 PM
    • Edited by SasiVatsa Wednesday, January 16, 2013 8:17 PM
    •  

All Replies

  • Wednesday, January 23, 2013 7:50 AM
     
     
    I think it is better to modify the java code. Since this forum is for SQL Server, for java code, it is better to ask Java experts for more help
  • Monday, January 28, 2013 10:41 PM
     
     
    getGeneratedKeys() jdbc method is not working when the Id is generated using NEWSEQUENTIALID() (see schema). I think it is MSSQL JDBC driver problem. I posted java code so that it is easier to check it if someone is looking at it. Hope someone will look into it.