简体   繁体   English

java - 使用数组调用PL / SQL存储过程

[英]java - Calling a PL/SQL Stored Procedure With Arrays

I have a PL/SQL stored procedure similar to the following that I need to call in Java: 我有一个类似于以下需要用Java调用的PL / SQL存储过程:

TYPE AssocArrayVarchar20_t   is table of VARCHAR2(20)   index by BINARY_INTEGER
TYPE AssocArrayVarchar4100_t is table of VARCHAR2(4100) index by BINARY_INTEGER
TYPE AssocArrayNumber_t      is table of NUMBER         index by BINARY_INTEGER

PROCEDURE DATA_WRITE( I_NAME IN AssocArrayVarchar20_t,
                      I_NUM  IN AssocArrayNumber_t,
                      I_NOTE IN AssocArrayVarchar4100_t)
    // Do Stuff
END DATA_WRITE;

I tried the following in Java: 我在Java中尝试了以下内容:

CallableStatement stmt = conn.prepareCall("begin DATA_WRITE(?, ?, ?); end;");
stmt.setArray(0, conn.createArrayOf("VARCHAR", new String[]{ name }));
stmt.setArray(1, conn.createArrayOf("NUMBER", new Integer[]{ num }));
stmt.setArray(2, conn.createArrayOf("VARCHAR2", new String[]{ notes }));
stmet.execute;

When I do this I get a SQLException: Unsupported Feature" on the createArrayOf() method. I've also tried setObject() and inside of createArrayOf : "varchar" , "AssocArrayVarchar20_t" , "varchar_t" . Nothing seems to change that outcome. 当我这样做时,我在createArrayOf()方法上得到一个SQLException: Unsupported Feature" 。我也尝试了setObject()createArrayOf"varchar""AssocArrayVarchar20_t""varchar_t" 。似乎没有什么改变这个结果。

Does anyone know what I'm doing wrong? 有谁知道我做错了什么? I can't seem to get it to work. 我似乎无法让它发挥作用。

UPDATE: Success! 更新:成功!

OracleCallableStatement pStmt = (OracleCallableStatement) conn.prepareCall("begin DATA_WRITE(?, ?, ?); end;");
pStmt.setPlsqlIndexTable(1, new String[]{ name }, 1, 1, OracleTypes.VARCHAR, 20);
pStmt.setPlsqlIndexTable(2, new Integer[]{ num }, 1, 1, OracleTypes.NUMBER, 0);
pStmt.setPlsqlIndexTable(3, new String[]{ notes }, 1, 1, OracleTypes.VARCHAR, 4100);
pStmt.execute();

The createArrayOf method was introduced in Java 1.6, but to the best of my knowledge it doesn't handle Oracle's PL/SQL associative arrays. createArrayOf方法是在Java 1.6中引入的,但据我所知,它不能处理Oracle的PL / SQL关联数组。 If you have the Oracle JDBC driver, then you have access to the oracle.sql classes. 如果您有Oracle JDBC驱动程序,则可以访问oracle.sql类。

You should be able to downcast the CallableStatement to an OracleCallableStatement . 您应该能够将CallableStatement向下转换为OracleCallableStatement From there you can call the setPlsqlIndexTable method and you should be able to pass in a Java array. 从那里你可以调用setPlsqlIndexTable方法 ,你应该能够传入一个Java数组。

Binds a PL/SQL index-by table parameter in the IN parameter mode. 在IN参数模式下绑定PL / SQL索引表参数。

Here is an official guide reference to pass Arrays in case you need to pass arrays and not tables: oracle guide 以下是传递Arrays的官方指南参考,以防您需要传递数组而不是表: oracle guide

Oracle JDBC does not support the JDBC 4.0 method createArrayOf method of java.sql.Connection interface. Oracle JDBC不支持java.sql.Connection接口的JDBC 4.0方法createArrayOf方法。 This method only allows anonymous array types, while all Oracle array types are named. 此方法仅允许匿名数组类型,而所有Oracle数组类型都已命名。 Use the Oracle specific method oracle.jdbc.OracleConnection.createARRAY instead. 请改用Oracle特定方法oracle.jdbc.OracleConnection.createARRAY。

Passing an Array to a Prepared Statement 将数组传递给准备好的语句

Pass an array to a prepared statement as follows. 将数组传递给预准备语句,如下所示。

Note: you can use arrays as either IN or OUT bind variables. 注意:您可以将数组用作IN或OUT绑定变量。 Define the array that you want to pass to the prepared statement as an oracle.sql.ARRAY object. 将要传递给预准备语句的数组定义为oracle.sql.ARRAY对象。

ARRAY array = oracle.jdbc.OracleConnection.createARRAY(sql_type_name, elements); ARRAY array = oracle.jdbc.OracleConnection.createARRAY(sql_type_name, elements); sql_type_name is a Java string specifying the user-defined SQL type name of the array and elements is a java.lang.Object containing a Java array of the elements. sql_type_name是一个Java字符串,指定数组的用户定义的SQL类型名称,而元素是包含元素的Java数组的java.lang.Object。

Create a java.sql.PreparedStatement object containing the SQL statement to be run. 创建一个包含要运行的SQL语句的java.sql.PreparedStatement对象。

Cast your prepared statement to OraclePreparedStatement, and use setARRAY to pass the array to the prepared statement. 将准备好的语句转换为OraclePreparedStatement,并使用setARRAY将数组传递给预准备语句。

(OraclePreparedStatement)stmt.setARRAY(parameterIndex, array); parameterIndex is the parameter index and array is the oracle.sql.ARRAY object you constructed previously. parameterIndex是参数索引,array是您之前构造的oracle.sql.ARRAY对象。

Run the prepared statement. 运行准备好的语句。

Note: by the 注意:由

ARRAY array = oracle.jdbc.OracleConnection.createARRAY(sql_type_name, elements); 

They mean: 他们的意思:

java.sql.Connection connection = ...
oracle.jdbc.OracleConnection oracleConnection = connection.unwrap(OracleConnection.class);
ARRAY array = oracleConnection.createARRAY(sql_type_name, elements); 

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

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