2

我一直遇到一个问题,在尝试从存储过程返回引用游标时,我不断收到java.sql.SQLException: Bigger type length than Maximum错误。

我有一个非常简单的存储过程,它只有一个输出变量作为引用游标,ref_cursor 被定义为名为 WS_SEARCHTEST 的 Oracle 包中的类型引用游标。

CREATE OR REPLACE PACKAGE DB.WS_SEARCHTEST
IS
TYPE ref_cursor IS REF CURSOR;

我的程序:

PROCEDURE searchTest(query_result OUT ref_cursor)
Is
Begin
   open query_result for select pay_id, member_id, group_no from member_table
where carrier_no = '7' AND group_no = '200607';
End;

我知道我的连接有效,因为我用它来运行简单的查询并检查了数据库。在我的 java 代码中,我执行以下操作:

callStmt = connection.prepareCall("{call WS_SEARCHTEST.searchTest(?)}");
callStmt.registerOutParameter(1, OracleTypes.CURSOR);
callStmt.execute();
resultSet = (ResultSet) callStmt.getObject(1);

我也试过:

 resultSet = callStmt.executeUpdate();    

 resultSet = callStmt.executeQuery();

但是,当我尝试访问结果集时,我总是会得到 java.sql.SQLException: Bigger type length than Maximum。

谢谢

4

1 回答 1

1
if you want to write a java code which will call PostgreSQL Stored procedure where 
there is an INOUT refcursor.
PostgreSQL SP:

 CREATE OR REPLACE PROCEDURE  Read_DataDetails(INOUT  my_cursor REFCURSOR = 'rs_resultone')
 LANGUAGE plpgsql
 AS $$                                                    
 BEGIN
 OPEN my_cursor FOR Select * from MyData;      
 END;
 $$;

 corresponding java code will be:

connection = getConnection(your connection parameter);
if (connection != null) {
connection.setAutoCommit(false); // This line must be written just after the 
//connection open otherwise it will not work since cursor will be closed immediately. 
String readMetaData = "CALL Read_DataDetails(?)";
callableStatement = connection.prepareCall(readMetaData);
callableStatement.registerOutParameter(1, Types.REF_CURSOR);
callableStatement.execute();
rs = (ResultSet) callableStatement.getObject(1);
if (rs != null) {
    while (rs.next()) {
                 <Your Logic>
                }//end while
            }//end if

}//end if
于 2020-06-18T12:44:23.700 回答