【问题标题】:Unable to call Redshift stored procedure using refcursor无法使用 refcursor 调用 Redshift 存储过程
【发布时间】:2021-11-26 13:12:23
【问题描述】:

我关注了这份文件https://docs.aws.amazon.com/redshift/latest/dg/stored-procedure-result-set.html 但似乎不完整,没有引用statement对象,该对象用于从refcursor中提取数据

我尝试了各种方法

PreparedStatement statement = connection.
                    prepareStatement("call myproc( null, null, 3, 4, 'rs' )");
      boolean bb = statement.execute();
ResultSet resultSet = statement.executeQuery("fetch all from rs")

上面的代码失败了

java.sql.SQLNonTransientException: [Amazon][JDBC](11200) Invalid operation for this statement object

还尝试创建一个新的prepareStatement对象

PreparedStatement statement = connection.
                    prepareStatement("call myproc( null, null, 3, 4, 'rs' )");
      boolean bb = statement.execute();
PreparedStatement p = connection.prepareStatement("fetch all from rs")
 ResultSet resultSet = p.executeQuery()

上面的语句失败了

"INTERNAL_SERVER_ERROR","message":"java.sql.SQLException: [Amazon](500310) Invalid operation: cursor \"rs\" does not exist;"

Gradle.properties 条目

     dependency(group: 'com.amazon.redshift', name: 'redshift-jdbc42-no-awssdk', version: '1.2.37.1061')
            dependencySet(group: "com.amazonaws", version: "1.11.688") {
                entry "aws-java-sdk-s3"
                entry "aws-java-sdk-sts"
                entry "aws-java-sdk-redshift"
            };

【问题讨论】:

    标签: java spring-boot amazon-redshift


    【解决方案1】:

    我又尝试了一些东西并获得了成功。

      try (PreparedStatement statement =
                     connection.prepareStatement
                             ("call myproc( null, null, 3, 4, 'rs' )")) {
            statement.execute();
            try (PreparedStatement p = connection.prepareStatement("fetch all from rs")) {
                try (ResultSet resultSet = p.executeQuery()) {
    
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    

    我不知道为什么这段代码有效。 refcursor 的一件事永远不要将 Autocommit 设置为 true。

    【讨论】:

      猜你喜欢
      • 2017-07-22
      • 1970-01-01
      • 2021-09-19
      • 2021-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-12-08
      • 1970-01-01
      • 2022-01-22
      相关资源
      最近更新 更多