使用JDBC从存储过程中获取多个记录时获取异常

时间:2014-07-10 19:03:58

标签: java sql oracle stored-procedures jdbc

我使用下面的代码使用存储过程从DB获取多条记录到JAVA。

public static void main(String[] args) throws ClassNotFoundException, SQLException {

    Class.forName("oracle.jdbc.OracleDriver");
    Connection con=DriverManager.getConnection("xxxx", "xxxx", "xxxx");
    String query="{call stud_proc(?,?)}";
    CallableStatement cstmt=con.prepareCall(query);
    cstmt.setString(1,"e001");
    cstmt.registerOutParameter(2,OracleTypes.CURSOR);
    cstmt.execute();

    ResultSet rs=(ResultSet)cstmt.getObject(2);
    while(rs.next()){
        System.out.println(rs.getString(1));
    }


}

存储过程如下所示:

CREATE OR REPLACE PROCEDURE stud_proc
(
EID_VAR IN VARCHAR2,
CUR_VAR OUT SYS_REFCURSOR
)
AS
BEGIN
OPEN CUR_VAR FOR
SELECT ENAME FROM EMP WHERE EID=EID_VAR;
END;

我得到以下异常:

Exception in thread "main" java.sql.SQLException: Bigger type length than Maximum
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:125)
at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:162)
at oracle.jdbc.driver.DatabaseError.check_error(DatabaseError.java:885)
at oracle.jdbc.driver.T4CMAREngine.buffer2Value(T4CMAREngine.java:2231)
at oracle.jdbc.driver.T4CMAREngine.unmarshalUB2(T4CMAREngine.java:1048)
at oracle.jdbc.driver.T4CTTIdcb.receiveCommon(T4CTTIdcb.java:112)
at oracle.jdbc.driver.T4CTTIdcb.receiveFromRefCursor(T4CTTIdcb.java:104)
at oracle.jdbc.driver.T4CResultSetAccessor.unmarshalOneRow(T4CResultSetAccessor.java:165)
at oracle.jdbc.driver.T4CTTIrxd.unmarshal(T4CTTIrxd.java:788)
at oracle.jdbc.driver.T4CTTIrxd.unmarshal(T4CTTIrxd.java:705)
at oracle.jdbc.driver.T4C8Oall.receive(T4C8Oall.java:527)
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:180)
at oracle.jdbc.driver.T4CCallableStatement.execute_for_rows(T4CCallableStatement.java:783)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1028)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:2888)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:2979)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4103)
at prac1.Main.main(Main.java:30)

我按照How to access this stored Procedure from JDBC Callablestatement?这个链接尝试了:

    String query="{call stud_proc(?,?)}";
    CallableStatement cstmt=con.prepareCall(query);
    cstmt.setString(1,"e001");
    cstmt.registerOutParameter(2,OracleTypes.CURSOR);
    cstmt.execute();


    ResultSet rs=((OracleCallableStatement)cstmt).getCursor(2);
    while(rs.next()){
        System.out.println(rs.getString(1));
}

但是我仍然得到同样的错误。这是与Oracle Version相关的数据库问题

0 个答案:

没有答案