简体   繁体   English

使用 JdbcTemplate 将 Java 数组传递给 ORACLE 存储过程

[英]Pass an Java Array to ORACLE Stored Procedure using JdbcTemplate

I am trying to pass Integer Array to Oracle Stored Procedure.我正在尝试将 Integer 数组传递给 Oracle 存储过程。

This is my oracle side:这是我的 oracle 端:

-- Package Declaration
TYPE num_array IS TABLE OF NUMBER;

PROCEDURE test_arrays(p_array IN num_array);

-- Package Body
PROCEDURE test_arrays(p_array IN num_array)
    IS
    BEGIN
        FOR i IN 1 .. p_array.count
        LOOP
            INSERT INTO GENERAL.TEST_ARRAYS VALUES (p_array(i));
        END LOOP;
        
    END;

So simple.很简单。 Now I need to pass an integer array from the database.现在我需要从数据库中传递一个 integer 数组。 This is my code:这是我的代码:

public void testArrays() {
        int[] intArray = new int[100];
        for(int i=0; i<100; i++) {
            intArray[i] = i;
        }
        
        SimpleJdbcCall simpleJdbcCall = new SimpleJdbcCall(this.jdbcTemplate)
                .withSchemaName("general")
                .withCatalogName("sms_package")
                .withProcedureName("test_arrays");

        SqlParameterSource parameterSource = new MapSqlParameterSource()
                .addValue("p_array", intArray, Types.ARRAY);

        simpleJdbcCall.execute(parameterSource);
    }

this throws an invalid column type.这会引发无效的列类型。

After a little research I saw that I need to create some array descriptors, I tried some codes:经过一番研究,我发现我需要创建一些数组描述符,我尝试了一些代码:


SqlTypeValue pvCodScg = new AbstractSqlTypeValue() {
            protected Object createTypeValue(Connection conn, int sqlType, String typeName) throws SQLException {
                ArrayDescriptor arrayDescriptor = new ArrayDescriptor(typeName, conn);
                return new ARRAY(arrayDescriptor, conn, intArray);
            }
};

and passed it as a:并将其作为:

SqlParameterSource parameterSource = new MapSqlParameterSource()
                .addValue("p_array", pvCodScg);

but this still doesn't work.但这仍然行不通。 Any help would be appreciated.任何帮助,将不胜感激。

Thanks谢谢

What do you pass as "typeName"?你传递什么作为“typeName”? It must be a known ORACLE type of the target schema: either something like SYS.ODCIVARCHAR2LIST, ... or a TYPE you defined (eg a TABLE OF...) so in your case probably "NUM_ARRAY".它必须是目标模式的已知 ORACLE 类型:类似于 SYS.ODCIVARCHAR2LIST,...或您定义的 TYPE(例如 TABLE OF...),因此在您的情况下可能是“NUM_ARRAY”。 (NB you could have used SYS.ODCINUMBERLIST) (I don't remember if passing lowercase works, so I prefer to always use UPPERCASE) NB when using directly JDBC, there is a PreparedSteament.setArray function, not sure MapSqlParameters is doing the right thing with a SqlTypeValue because I saw it's testing "instanceof SqlParameterValue". (注意你可以使用 SYS.ODCINUMBERLIST)(我不记得传递小写是否有效,所以我更喜欢总是使用大写) 注意直接使用 JDBC 时,有一个 PreparedSteament.setSqlArray function,不确定 Map 是否正确使用 SqlTypeValue 的东西,因为我看到它正在测试“instanceof SqlParameterValue”。

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

相关问题 无法将数组传递给Oracle中的存储过程 - Cannot pass array to stored procedure in oracle 如何将BLOB数组传递给存储的oracle过程? - How to pass an array of BLOB to a stored oracle procedure? 将oracle对象类型传递给j​​ava存储过程 - Pass oracle object type to java stored procedure java - 在oracle存储过程中传递数组 - java - passing array in oracle stored procedure Oracle 11g:使用简单的 jdbc 调用将数组作为输入参数传递给 oracle 存储过程 - Oracle 11g: Pass array as input parameter to an oracle stored procedure using simple jdbc call 将数组从Java传递到PLSQL存储过程 - Pass Array From Java to PLSQL Stored Procedure 如何使用ojdbc 12.1.0.2驱动程序将数组从Java传递到oracle存储过程? - How to pass array to oracle stored procedure from java with ojdbc 12.1.0.2 driver? 无法将数组(BINARY_DOUBLE)从Java传递到Oracle存储过程? - not possible to pass array (of BINARY_DOUBLE) from Java to Oracle stored procedure? Spring JdbcTemplate可以等待oracle存储过程完成多长时间 - how long can Spring JdbcTemplate wait for an oracle stored procedure to finish 使用简单的jdbc调用将数组作为输入参数传递给oracle存储过程 - Pass array as input parameter to an oracle stored procedure using simple jdbc call
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM