欢乐连接 executeUpdateAndGetGeneratedKeys 与 Oracle 的使用

mirth connect use of executeUpdateAndGetGeneratedKeys with Oracle

我正在使用 Mirth Connect 3.5.0.8232。我创建了一个到 Oracle 数据库的持久连接,并在我的源连接器和目标连接器中使用它。 Mirth 提供的与数据库对话的方法之一是 executeUpdateAndGetGeneratedKeys。这对于将 return 作为插入行的主键的插入语句非常有用。

我的问题是 - 您如何指定 return 的哪些列? 运行 提供的函数有效,但 returnCachedRowSet 中的 ROWID,这不是我想要的。

据我了解,return 哪些列取决于数据库的类型,并且每个数据库的行为都不同。我对 Oracle 特别感兴趣。

谢谢。

executeUpdateAndGetGeneratedKeys method uses the Statement.RETURN_GENERATED_KEYS flag to signal to the driver that auto-generated keys should be returned. However, from the Oracle docs:

If key columns are not explicitly indicated, then Oracle JDBC drivers cannot identify which columns need to be retrieved. When a column name or column index array is used, Oracle JDBC drivers can identify which columns contain auto-generated keys that you want to retrieve. However, when the Statement.RETURN_GENERATED_KEYS integer flag is used, Oracle JDBC drivers cannot identify these columns. When the integer flag is used to indicate that auto-generated keys are to be returned, the ROWID pseudo column is returned as key. The ROWID can be then fetched from the ResultSet object and can be used to retrieved other columns.

因此,尝试使用他们的建议,将列名数组传递给 prepareStatement:

var dbConn;

try {
    dbConn = DatabaseConnectionFactory.createDatabaseConnection('oracle.jdbc.driver.OracleDriver','jdbc:oracle:thin:@localhost:1521:DBNAME','user','pass');

    // Create a Java String array directly
    var keyColumns = java.lang.reflect.Array.newInstance(java.lang.String, 1);
    keyColumns[0] = 'id';
    var ps = dbConn.getConnection().prepareStatement('INSERT INTO tablename (columnname) VALUES (?)', keyColumns);

    try {
        // Set variables here
        ps.setObject(1, 'test');
        ps.executeUpdate();

        var result = ps.getGeneratedKeys();
        result.next();
        var generatedKey = result.getObject(1);

        logger.info(generatedKey);
    } finally {
        ps.close();
    }
} finally {
    if (dbConn) {
        dbConn.close();
    }
}