PLSQL JDBC:如何获取最后一行ID?

什么是PLSQL(甲骨文)相当于这个SQL服务器片段?

BEGIN TRAN INSERT INTO mytable(content) VALUES ("test") -- assume there's an ID column that is autoincrement SELECT @@IDENTITY COMMIT TRAN 

在C#中,您可以调用myCommand.ExecuteScalar()来检索新行的ID。

我怎样才能在Oracle中插入一个新的行,并让JDBC获得一个新的ID的副本?

编辑: BalusC提供了一个非常好的起点。 出于某种原因,JDBC不喜欢命名参数绑定。 这给“错误地设置或注册参数”SQLException。 为什么发生这种情况?

  OracleConnection conn = getAppConnection(); String q = "BEGIN INSERT INTO tb (id) values (claim_seq.nextval) returning id into :newId; end;" ; CallableStatement cs = (OracleCallableStatement) conn.prepareCall(q); cs.registerOutParameter("newId", OracleTypes.NUMBER); cs.execute(); int newId = cs.getInt("newId"); 

通常情况下,您可以使用Statement#getGeneratedKeys()来实现此目的(另请参见此答案的示例),但Oracle JDBC驱动程序仍然不支持此操作。

您最好的select是使用带有RETURNING子句的CallableStatement

 String sql = "BEGIN INSERT INTO mytable(id, content) VALUES (seq_mytable.NEXTVAL(), ?) RETURNING id INTO ?; END;"; Connection connection = null; CallableStatement statement = null; try { connection = database.getConnection(); statement = connection.prepareCall(sql); statement.setString(1, "test"); statement.registerOutParameter(2, Types.NUMERIC); statement.execute(); int id = statement.getInt(2); // ... 

或者在同一事务中INSERT之后触发SELECT sequencename.CURRVAL

 String sql_insert = "INSERT INTO mytable(content) VALUES (?)"; String sql_currval = "SELECT seq_mytable.CURRVAL FROM dual"; Connection connection = null; PreparedStatement statement = null; Statement currvalStatement = null; ResultSet currvalResultSet = null; try { connection = database.getConnection(); connection.setAutoCommit(false); statement = connection.prepareStatement(sql_insert); statement.setString(1, "test"); statement.executeUpdate(); currvalStatement = connection.createStatement(); currvalResultSet = currvalStatement.executeQuery(sql_currval); if (currvalResultSet.next()) { int id = currvalResultSet.getInt(1); } connection.commit(); // ... 

您可以使用Oracle的返回子句。

 insert into mytable(content) values ('test') returning your_id into :var; 

查看代码示例的链接 。 您需要Oracle 10g或更高版本以及新版本的JDBC驱动程序。

您可以使用getGeneratedKeys(),通过显式select关键字段。 这是一个片段:

  // change the string to your connection string Connection connection = DriverManager.getConnection("connection string"); // assume that the field "id" is PK, and PK-trigger exists String sql = "insert into my_table(id) values (default)"; // you can select key field by field index int[] colIdxes = { 1 }; // or by field name String[] colNames = { "id" }; // Java 1.7 syntax; try-finally for older versions try (PreparedStatement preparedStatement = connection.prepareStatement(sql, colNames)) { // note: oracle JDBC driver do not support auto-generated key feature with batch update // // insert 5 rows // for (int i = 0; i < 5; i++) // { // preparedStatement.addBatch(); // } // // int[] batch = preparedStatement.executeBatch(); preparedStatement.executeUpdate(); // get generated keys try (ResultSet resultSet = preparedStatement.getGeneratedKeys()) { while (resultSet.next()) { // assume that the key's type is BIGINT long id = resultSet.getLong(1); assertTrue(id != 0); System.out.println(id); } } } 

请参阅: http : //docs.oracle.com/cd/E16655_01/java.121/e17657/jdbcvers.htm#CHDEGDHJ