Java用ref游标返回类型调用存储过程

时间:2014-03-17 16:37:42

标签: java stored-procedures jdbc plsql callable-statement

我遇到了一个问题,当我尝试从我的存储过程中返回一个引用游标时,我经常遇到 java.sql.SQLException:比最大更大的类型长度。

我有一个非常简单的存储过程,只有一个输出变量作为引用游标,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:当我尝试访问resultSet时,类型长度大于Maximum。

由于

1 个答案:

答案 0 :(得分: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