[英]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.