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



我正试图将一个整数数组传递给Oracle存储过程。

这是我的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;

这么简单。现在,我需要从数据库中传递一个整数数组。这是我的代码:

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);
}

这引发了一个无效的列类型。

经过一点研究,我发现我需要创建一些数组描述符,我尝试了一些代码:


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);
}
};

我把它当作:

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

这仍然不起作用。

您传递什么作为"typeName"它必须是目标模式的已知ORACLE类型:要么是像SYS.ODCIVARCHAR2LIST之类的东西,…要么是你定义的类型(例如TABLE of…(,所以在你的情况下可能是";NUM_ARRAY";。(注意,您本可以使用SYS.ODCINUMBERLIST((我不记得传递小写字母是否有效,所以我更喜欢始终使用大写字母(注意,当直接使用JDBC时,有一个PreparedSteament.setArray函数,不确定MapSqlParameters是否使用SqlTypeValue做了正确的事情,因为我看到它正在测试";instanceofSqlParameterValue";。

最新更新