Primary key from inserted row jdbc?

后端 未结 7 1228
野趣味
野趣味 2020-12-05 06:23

Is there a cross database platform way to get the primary key of the record you have just inserted?

I noted that this answer says that you can get it by Calling

相关标签:
7条回答
  • 2020-12-05 07:15

    Copied from my code:

    pInsertOid = connection.prepareStatement(INSERT_OID_SQL, Statement.RETURN_GENERATED_KEYS);
    

    where pInsertOid is a prepared statement.

    you can then obtain the key:

    // fill in the prepared statement and
    pInsertOid.executeUpdate();
    ResultSet rs = pInsertOid.getGeneratedKeys();
    if (rs.next()) {
      int newId = rs.getInt(1);
      oid.setId(newId);
    }
    

    Hope this gives you a good starting point.

    0 讨论(0)
提交回复
热议问题