Primary key from inserted row jdbc?

后端 未结 7 1244
野趣味
野趣味 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 06:55

    extraneon's answer, although correct, doesn't work for Oracle.

    The way you do this for Oracle is:

    String key[] = {"ID"}; //put the name of the primary key column
    
    ps = con.prepareStatement(insertQuery, key);
    ps.executeUpdate();
    
    rs = ps.getGeneratedKeys();
    if (rs.next()) {
        generatedKey = rs.getLong(1);
    }
    

提交回复
热议问题