Java 通过JDBC从AWS红移检索插入的标识值

Java 通过JDBC从AWS红移检索插入的标识值,java,postgresql,jdbc,insert,amazon-redshift,Java,Postgresql,Jdbc,Insert,Amazon Redshift,在使用AWS Redshift is时,我注意到通过从带有标识列的表中获取最后插入的id JDBC驱动程序无法使用以下任一方法完成: RETURNING key word 或 如堆栈溢出项中所述: 上述方法不可用,因为红移(截至2013年10月17日)是基于PostgreSQL 8.0.2版构建的。见下文 以下链接中的文档: 如果您打算将红移用作RDBMS,那么阅读以下内容也是值得的: 问题: 通过以下方式检索红移中自动递增/串行/标识列上最后插入的id的最佳策略是什么 PostgreS

在使用AWS Redshift is时,我注意到通过从带有标识列的表中获取最后插入的id JDBC驱动程序无法使用以下任一方法完成:

RETURNING key word

如堆栈溢出项中所述:

上述方法不可用,因为红移(截至2013年10月17日)是基于PostgreSQL 8.0.2版构建的。见下文 以下链接中的文档:

如果您打算将红移用作RDBMS,那么阅读以下内容也是值得的:

问题:

通过以下方式检索红移中自动递增/串行/标识列上最后插入的id的最佳策略是什么
PostgreSQL JDBC驱动程序?

鉴于红移引擎是在PostgreSQL 8.0.2上构建的,并且上面的RETURNING和Statement.RETURN\u生成的\u键选项不可用 可用且红移不支持创建序列以使用CURRVAL/NEXTVAL功能套件,一个选项是 将两个SQL语句组合在一起,在JDBC事务中插入并选择MAX([identity column])

try {

    // create the JDBC connection
    Class.forName(JDBC_DRIVER);
    Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);

    // start the transaction
    conn.setAutoCommit(false);  
    // create the prepared statement for insert
    PreparedStatement prpd = conn.prepareStatement(SQL_INSERT_STATEMENT);  

    // set input/output parameters as needed...

    // execute the SQL prepared statement
    int j = prpd.executeUpdate();

    // create a statement for select max()
    Statement stmt = conn.createStatement(); 
    // execute the statement to return a result set           
    ResultSet key = stmt.executeQuery(SQL_SELECT_MAX_STATEMENT);

    // initialize and retrieve the incremented identity value, in this case it is a long (bigint data type in Redshift)
    long id = 0;
    if (key.next()) {
        id = key.getLong(1);
    }

    // commit the entire transaction        
    conn.commit();

} catch (SQLException se) {
    // if an SQL exception occurs, rollback the whole deal
    try {
        if (conn!=null && !conn.isClosed()) {
            conn.rollback();
        }

    } catch (Exception e) {

    }
} catch (Exception e) {
    // roll back if something other than an SQLException occurs
    try {
        if (conn!=null && !conn.isClosed()) {
            conn.rollback();
        }
    } catch (Exception e) {

    }
} finally {
    // do whatever you want to return a value, shut down resources
    // close out JDBC resources
    try {
        if (conn!=null && !conn.isClosed()) {
            conn.setAutoCommit(true);
        }                
    } catch (SQLException se) {

    }

    try {                
        if (prpd!=null && !prpd.isClosed()) {
            prpd.close();
        }                                
    } catch (SQLException se) {

    }

    try {                
        if (stmt!=null && !stmt.isClosed()) {
            stmt.close();
        }                                
    } catch (SQLException se) {

    }

    try {
        if (conn!=null && !conn.isClosed()) {
            conn.close();
        }
    } catch (SQLException se) {

    }
}
如果SQL_INSERT_语句写入/锁定单个表,则上述操作将起作用。多个表锁将需要一个同步的 关键字来防止死锁。在锁定的表上进行选择将允许添加递增的标识值 在结果集中返回

try {

    // create the JDBC connection
    Class.forName(JDBC_DRIVER);
    Connection conn = DriverManager.getConnection(DB_URL, USER, PASSWORD);

    // start the transaction
    conn.setAutoCommit(false);  
    // create the prepared statement for insert
    PreparedStatement prpd = conn.prepareStatement(SQL_INSERT_STATEMENT);  

    // set input/output parameters as needed...

    // execute the SQL prepared statement
    int j = prpd.executeUpdate();

    // create a statement for select max()
    Statement stmt = conn.createStatement(); 
    // execute the statement to return a result set           
    ResultSet key = stmt.executeQuery(SQL_SELECT_MAX_STATEMENT);

    // initialize and retrieve the incremented identity value, in this case it is a long (bigint data type in Redshift)
    long id = 0;
    if (key.next()) {
        id = key.getLong(1);
    }

    // commit the entire transaction        
    conn.commit();

} catch (SQLException se) {
    // if an SQL exception occurs, rollback the whole deal
    try {
        if (conn!=null && !conn.isClosed()) {
            conn.rollback();
        }

    } catch (Exception e) {

    }
} catch (Exception e) {
    // roll back if something other than an SQLException occurs
    try {
        if (conn!=null && !conn.isClosed()) {
            conn.rollback();
        }
    } catch (Exception e) {

    }
} finally {
    // do whatever you want to return a value, shut down resources
    // close out JDBC resources
    try {
        if (conn!=null && !conn.isClosed()) {
            conn.setAutoCommit(true);
        }                
    } catch (SQLException se) {

    }

    try {                
        if (prpd!=null && !prpd.isClosed()) {
            prpd.close();
        }                                
    } catch (SQLException se) {

    }

    try {                
        if (stmt!=null && !stmt.isClosed()) {
            stmt.close();
        }                                
    } catch (SQLException se) {

    }

    try {
        if (conn!=null && !conn.isClosed()) {
            conn.close();
        }
    } catch (SQLException se) {

    }
}