Java: Passing Array to Oracle Stored Procedure
来源:互联网 发布:网络机柜配线架跳线图 编辑:程序博客网 时间:2024/04/29 21:09
All PLSQL arrays can not be called from java. An array needs to be created as TYPE
, at SCHEMA level in the database and then it can be used with ArrayDescriptor in Java, asoracle.sql.ArrayDescriptor
class in Java can not access at package level.
Database Code
First, Create an array, at SCHEMA level. An example is shown below:
CREATE
TYPE array_table
AS
TABLE
OF
VARCHAR2 (50);
-- Array of String
CREATE
TYPE array_int
AS
TABLE
OF
NUMBER;
-- Array of integers
Next, Create a procedure which takes an array as an input parameter and returns an array as its OUT parameter.
An example of one such procedure is shown below, which has 2 parameters -
- an array of String as its IN parameter – p_array
- an array of Integers as OUT parameter – p_arr_int
CREATE OR REPLACE PROCEDURE SchemaName.proc1 (p_array IN array_table, len OUT NUMBER, p_arr_int OUT array_int)AS v_count NUMBER;BEGIN p_arr_int := NEW array_int (); p_arr_int.EXTEND (10); len := p_array.COUNT; v_count := 0; FOR i IN 1 .. p_array.COUNT LOOP DBMS_OUTPUT.put_line (p_array (i)); p_arr_int (i) := v_count; v_count := v_count + 1; END LOOP;END;/
GRANT EXECUTE ON SchemaNAme.proc1 TO UserName;
Java Code
Create a java class which makes a call to the procedure proc1
, created before.
Below is an example which contains the whole flow from creating a connection with the database, to making a call to the stored procedure, passing an array to Oracle procedure, retrieving an array from an Oracle procedure and displaying the result.
import java.math.BigDecimal;import java.sql.CallableStatement;import java.sql.Connection;import java.sql.DriverManager;import java.sql.Types;import oracle.jdbc.OracleCallableStatement;import oracle.jdbc.internal.OracleTypes;import oracle.sql.ARRAY;import oracle.sql.ArrayDescriptor; public class TestDatabase { public static void passArray() { try{ Class.forName("oracle.jdbc.OracleDriver"); Connection con = DriverManager.getConnection("jdbc:oracle:thin:url ","UserName","Password");; String array[] = {"one", "two", "three","four"}; ArrayDescriptor des = ArrayDescriptor.createDescriptor("SchemaName.ARRAY_TABLE", con); ARRAY array_to_pass = new ARRAY(des,con,array); CallableStatement st = con.prepareCall("call SchemaName.proc1(?,?,?)"); // Passing an array to the procedure - st.setArray(1, array_to_pass); st.registerOutParameter(2, Types.INTEGER); st.registerOutParameter(3,OracleTypes.ARRAY,"SchemaName.ARRAY_INT"); st.execute(); System.out.println("size : "+st.getInt(2)); // Retrieving array from the resultset of the procedure after execution - ARRAY arr = ((OracleCallableStatement)st).getARRAY(3); BigDecimal[] recievedArray = (BigDecimal[])(arr.getArray()); for(int i=0;i<recievedArray.length;i++) System.out.println("element" + i + ":" + recievedArray[i] + "\n"); } catch(Exception e) { System.out.println(e); } } public static void main(String args[]){ passArray(); }}
补充:数组,对象等必须要在TYPE里面定义。
文章来自:http://viralpatel.net/blogs/java-passing-array-to-oracle-stored-procedure/
- Java: Passing Array to Oracle Stored Procedure
- Java: Passing Array To Oracle Stored Procedure
- ORA-06550 & PLS-00306 passing a parameter array to a stored procedure
- Passing an array or DataTable into a stored procedure
- DB2 Passing transition tables from trigger to stored procedure
- pass blob to oracle stored procedure
- Oracle Java Stored Procedure调用Unix shell
- Oracle Java Stored Procedure + 包调用
- oracle, stored procedure, cursor
- Oracle Stored Procedure example
- Java - Stored Procedure OUT Parameter
- navicate how to create a stored procedure
- navicate how to create a stored procedure
- How to call stored procedure in Hibernate
- How to call stored procedure in Hibernate
- How To Use a DataReader Against an Oracle Stored Procedure in Visual C# .NET
- How to call Oracle function or stored procedure using spring persistence framework?
- Get Dataset from Stored Procedure in Oracle
- Struts2+Hibernate3+Spring3简单整合练习
- Ubuntu双显卡解决方案(Nvidia)
- js 倒数计时器
- MFC中使用otl,oci链接oracle数据库
- mysql优化Analyze Table
- Java: Passing Array to Oracle Stored Procedure
- 程序员浪费生命的几种方式
- opencv1.0
- Hashmap
- iOS开发获取屏幕的宽高
- 编程过程中的错误处理
- WINDOWS进程或线程号为什么是4的倍数(PspCidTable的含义)
- DOM对象和JQuery对象的区别
- NDK编译问题