使用简单的jdbc调用将数组作为输入参数传递给oracle存储过程

时间:2014-11-06 07:15:56

标签: java spring stored-procedures spring-jdbc

我正在关注此链接http://docs.spring.io/spring-data/jdbc/docs/1.1.0.RELEASE/reference/html/orcl.datatypes.html#d0e2387

这是我的oracle程序规范

CREATE OR REPLACE PACKAGE PKG_RE_FI AS

  PROCEDURE PRC_RE_FI_DETAILS(P_FAN_NO       IN VARCHAR2,
                              P_REF_ID       IN TY_APP_REF_ID,
                              P_COMMENTS     IN VARCHAR2,
                              P_BILLING_FLAG IN VARCHAR2,
                              P_STATUS       OUT VARCHAR2);

END PKG_RE_FI;TY_APP_REF_ID

    CREATE OR REPLACE TYPE ty_app_REF_ID as varray(500) of obj_array_ref_id

    CREATE OR REPLACE TYPE obj_array_ref_id  AS OBJECT(
    app_ref_id VARCHAR2(100)

)

我使用Spring JDBC Framework(SimpleJdbcCall对象)来执行上述过程。下面是我声明的代码片段

this.reFIJdbcCall =  new SimpleJdbcCall(dataSource).withCatalogName("PKG_RE_FI").
                      withProcedureName("PRC_RE_FI_DETAILS").withoutProcedureColumnMetaDataAccess().declareParameters(new SqlParameter("P_FAN_NO", Types.VARCHAR),
                                new SqlParameter("P_REF_ID", OracleTypes.ARRAY, "TY_APP_REF_ID"),
                                new SqlParameter("P_COMMENTS", Types.VARCHAR),
                                new SqlParameter("P_BILLING_FLAG", Types.VARCHAR),
                                new SqlOutParameter("P_STATUS", Types.VARCHAR)
                      );

@Override
public ReFIResponse reInitiateFI(ReFIRequest reFIRequest) {
     MapSqlParameterSource in = new MapSqlParameterSource();
    // Map in = Collections.singletonMap("in_actor_ids", new SqlArrayValue(ids)); 
       in.addValue("P_FAN_NO",reFIRequest.getFanNo());  
       String[] refIDS = new String[reFIRequest.getApplicantReferenceID().size()];
       refIDS = reFIRequest.getApplicantReferenceID().toArray(refIDS);
       in.addValue("P_REF_ID", new SqlArrayValue(refIDS));
       in.addValue("P_COMMENTS", reFIRequest.getComments());
       in.addValue("P_BILLING_FLAG", reFIRequest.getFiBillingFlag());
       Map  result = reFIJdbcCall.execute(in);
       String status = (String)result.get("P_STATUS");

    return null;
}

我收到以下错误

Exception in thread "main" org.springframework.jdbc.UncategorizedSQLException: CallableStatementCallback; uncategorized SQLException for SQL [{call PKG_RE_FI.PRC_RE_FI_DETAILS(?, ?, ?, ?, ?)}]; SQL state [null]; error code [17059]; Fail to convert to internal representation: TMFI10000031A; nested exception is java.sql.SQLException: Fail to convert to internal representation: TMFI10000031A
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:83)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:80)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:969)
    at org.springframework.jdbc.core.JdbcTemplate.call(JdbcTemplate.java:1003)
    at org.springframework.jdbc.core.simple.AbstractJdbcCall.executeCallInternal(AbstractJdbcCall.java:391)
    at org.springframework.jdbc.core.simple.AbstractJdbcCall.doExecute(AbstractJdbcCall.java:354)
    at org.springframework.jdbc.core.simple.SimpleJdbcCall.execute(SimpleJdbcCall.java:181)
    at com.tcs.fi.dao.ReFIDAOImpl.reInitiateFI(ReFIDAOImpl.java:77)
    at com.tcs.fi.business.FIReInitiator.reInitiateFI(FIReInitiator.java:57)
    at com.tcs.fi.business.FIReInitiator.reInitiateFI(FIReInitiator.java:39)
    at com.tcs.fi.business.Test.main(Test.java:11)
Caused by: java.sql.SQLException: Fail to convert to internal representation: TMFI10000031A
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112)
    at oracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:146)
    at oracle.jdbc.oracore.OracleTypeADT.toDatum(OracleTypeADT.java:239)
    at oracle.jdbc.oracore.OracleTypeADT.toDatumArray(OracleTypeADT.java:274)
    at oracle.jdbc.oracore.OracleTypeUPT.toDatumArray(OracleTypeUPT.java:115)
    at oracle.sql.ArrayDescriptor.toOracleArray(ArrayDescriptor.java:1314)
    at oracle.sql.ARRAY.<init>(ARRAY.java:152)
    at org.springframework.data.jdbc.support.oracle.SqlArrayValue.createTypeValue(SqlArrayValue.java:91)
    at org.springframework.jdbc.core.support.AbstractSqlTypeValue.setTypeValue(AbstractSqlTypeValue.java:58)
    at org.springframework.jdbc.core.StatementCreatorUtils.setValue(StatementCreatorUtils.java:267)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValueInternal(StatementCreatorUtils.java:216)
    at org.springframework.jdbc.core.StatementCreatorUtils.setParameterValue(StatementCreatorUtils.java:127)
    at org.springframework.jdbc.core.CallableStatementCreatorFactory$CallableStatementCreatorImpl.createCallableStatement(CallableStatementCreatorFactory.java:212)
    at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:947)

请帮助。

1 个答案:

答案 0 :(得分:1)

我用

解决了它
Map in = new HashMap();


       in.put("P_FAN_NO",reFIRequest.getFanNo());  
       Object[][] refIDS = new String[reFIRequest.getApplicantReferenceID().size()][1];

      for(int i = 0 ; i < reFIRequest.getApplicantReferenceID().size() ; i++){
         refIDS[i][0] = reFIRequest.getApplicantReferenceID().get(i);
      }
       in.put("P_REF_ID", new SqlArrayValue(refIDS));
       in.put("P_COMMENTS", reFIRequest.getComments());
       in.put("P_BILLING_FLAG", reFIRequest.getFiBillingFlag());
       Map  result = reFIJdbcCall.execute(in);
       String status = (String)result.get("P_STATUS");

似乎期待2D阵列。