简体   繁体   English

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

[英]Oracle 11g: Pass array as input parameter to an oracle stored procedure using simple jdbc call

I have the following stored proc:我有以下存储过程:

PROCEDURE test( p_a IN VARCHAR_ARRAY, p_b VARCHAR_ARRAY, p_c IN VARCHAR_ARRAY, 
p_d VARCHAR_ARRAY , p_e NUMBER_ARRAY,  p_f IN NUMBER_ARRAY, p_Results OUT SYS_REFCURSOR);

where在哪里

TYPE NUMBER_ARRAY IS TABLE OF NUMBER INDEX BY BINARY_INTEGER;
type VARCHAR_ARRAY IS TABLE OF VARCHAR2(1000) INDEX BY BINARY_INTEGER;

Now, I'm trying to call this proc from java using SimpleJdbcCallOperations .现在,我正在尝试使用SimpleJdbcCallOperations从 java 调用这个过程。

SimpleJdbcCallOperations jdbcCall = new SimpleJdbcCall(jdbcTemplate)
                .withSchemaName(schema)
                .withCatalogName(catalog);
final Result result = (Result) response.results().get(Constants.Dal.RESULTS);
final RowMapper<T> rowMapper = result.mapper();
jdbcCall.returningResultSet(Constants.Dal.RESULTS, rowMapper);
SqlParameterSource in = new SqlParameterSource();
in.addValue("p_a", new SqlArrayValue(a), Types.ARRAY, "VARCHAR_ARRAY");
in.addValue("p_b", new SqlArrayValue(b), Types.ARRAY, "VARCHAR_ARRAY");
in.addValue("p_c", new SqlArrayValue(c), Types.ARRAY, "VARCHAR_ARRAY");
in.addValue("p_d", new SqlArrayValue(d), Types.ARRAY, "VARCHAR_ARRAY");
in.addValue("p_e", new SqlArrayValue(e), Types.ARRAY, "NUMBER_ARRAY");
in.addValue("p_f", new SqlArrayValue(f), Types.ARRAY, "NUMBER_ARRAY");

final List<T> results = (List<T>) jdbcCall.executeObject(response.clazz(), in);

where a,b,c,d are String[] and e,f are BigDecimal[]其中 a,b,c,d 是 String[] 和 e,f 是 BigDecimal[]

But I get the following error:但我收到以下错误:

CallableStatementCallback; CallableStatementCallback; uncategorized SQLException for SQL [{call test(?, ?, ?, ?, ?, ?, ?)}]; SQL 的未分类 SQLException [{call test(?, ?, ?, ?, ?, ?, ?)}]; SQL state [99999]; SQL 状态 [99999]; error code [17059];错误代码 [17059]; Fail to convert to internal representation: [Ljava.lang.String;@5dadd44e;无法转换为内部表示:[Ljava.lang.String;@5dadd44e; nested exception is java.sql.SQLException: Fail to convert to internal representation: [Ljava.lang.String;@5dadd44e"嵌套异常是 java.sql.SQLException: Fail to convert to internal representation: [Ljava.lang.String;@5dadd44e"

I've tried many combinations.. using List<> instead of [] and double instead of BigDecimal, different Types, ... Any help would be highly appreciated.我尝试了很多组合.. 使用 List<> 而不是 [] 和 double 而不是 BigDecimal,不同的类型,......任何帮助将不胜感激。

UPDATE:更新:

Types are declared inside stored proc.类型在存储过程中声明。 and i tried adding params by specifying the schema, package name, like this: in.addValue("p_a", new SqlArrayValue(a), Types.ARRAY, "SCHEMANAME.PACKAENAME.VARCHAR_ARRAY");我尝试通过指定架构、包名称来添加参数,如下所示: in.addValue("p_a", new SqlArrayValue(a), Types.ARRAY, "SCHEMANAME.PACKAENAME.VARCHAR_ARRAY");

Oracle version : 11.2.0.3.0 (11g) Oracle 版本:11.2.0.3.0 (11g)

@Lukas Eder mentioned in comment that only 12c supports Associative arrays. @Lukas Eder 在评论中提到只有 12c 支持关联数组。 How do I achieve this in 11g?我如何在 11g 中实现这一目标?

Found the solution.找到了解决办法。 Have to convert the Java Arrays to Oracle native Arrays before passing them:在传递它们之前,必须将 Java 数组转换为 Oracle 本机数组

MapSqlParameterSource inParams = new MapSqlParameterSource();    
try {
    DataSource ds = jdbcTemplate.getDataSource();
    Connection conn = DataSourceUtils.getConnection(ds);
    OracleConnection oracleConnection = null;
    if (conn.isWrapperFor(OracleConnection.class))
        oracleConnection = conn.unwrap(OracleConnection.class);

    ARRAY aArrayValue = oracleConnection.createARRAY("VARCHAR_ARRAY", a.toArray());
    ARRAY bArray = oracleConnection.createARRAY("VARCHAR_ARRAY", b.toArray());
    ARRAY cArray = oracleConnection.createARRAY("VARCHAR_ARRAY", c.toArray());
    ARRAY dArray = oracleConnection.createARRAY("VARCHAR_ARRAY", d.toArray());
    ARRAY eArray = oracleConnection.createARRAY("NUMBER_ARRAY", e.toArray());
    ARRAY fArray = oracleConnection.createARRAY("NUMBER_ARRAY", f.toArray());

    inParams.addValue("p_a", aArrayValue);
    inParams.addValue("p_b", bArray);
    inParams.addValue("p_c", cArray);
    inParams.addValue("p_d", dArray);
    inParams.addValue("p_e", eArray);
    inParams.addValue("p_f", fArray);

} catch (SQLException e) {
    e.printStackTrace();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM