如何在Java中调用此PLSQL过程?

时间:2014-10-09 11:37:19

标签: java oracle hibernate plsql

我有这个PLSQL程序:

procedure GetContractInfo(RequestID number, ParamNames out TVarcharArray, ParamValues out TVarcharArray,
                         SessionID in IRBiS_Const.TSessionID default null);

TVarcharArray是:

type TVarcharArray is table of varchar2(255) index by binary_integer;

我想用Java调用它。这就是我的方式:

            sessionFactory.getCurrentSession().doWork(new Work() {
                public void execute(Connection con) throws SQLException {
                    OracleCallableStatement callableStatement = null;
                    try {
                        callableStatement = (OracleCallableStatement) con
                                .prepareCall("begin PREQUEST.GetContractInfo(?,?,?,?); end;");
                        callableStatement.setInt(1, request_id);
                        callableStatement.setString(4, sessionId);



                        //callableStatement.registerOutParameter(2, Types.ARRAY, "PREQUEST.TVarcharArray");
                        //callableStatement.registerOutParameter(3, Types.ARRAY, "PREQUEST.TVarcharArray");

                        callableStatement.registerIndexTableOutParameter(2, 16,
                                OracleTypes.VARCHAR, 0);

                        callableStatement.registerIndexTableOutParameter(3, 16,
                                OracleTypes.VARCHAR, 0);                    
                        callableStatement.execute();

                        System.out.println(callableStatement.getARRAY(2)
                                .getArray() == null); // NullPointer                    
                    } finally {
                        if (callableStatement != null) {
                            callableStatement.close();
                        }
                    }
                }
            });             

NullPointerException就是我所拥有的一切。可能有什么不对?

1 个答案:

答案 0 :(得分:2)

将第2和第3个参数设置为OracleTypes.ARRAY,如下所示。对于自定义类型

callableStatement.registerOutParameter(2, 
                                OracleTypes.ARRAY, "PREQUEST.TVARCHARARRAY");

callableStatement.registerOutParameter(3,
                           OracleTypes.ARRAY, "PREQUEST.TVARCHARARRAY");