Java: Passing Array to Oracle Stored Procedure
- By Vibhati on August 29, 2012
This tutorial guides us on how to pass Array objects from Java to stored procedures in Oracle and also, how to retrieve an array object in Java.
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, as oracle.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; /
After this, Execution permission would be required to execute the procedure created by you:
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(); } }
Brief Explanations:
Class.forName()
– Returns the Class object associated with the class or interface with the given string name.DriverManager.getConnection()
– Attempts to establish a connection to the given database URL.oracle.sql.ArrayDescriptor
β Describes an array classArrayDescriptor.createDescriptor()
– Descriptor factory. Lookup the name in the database, and determine the characteristics of this array.oracle.sql.ARRAY
– An Oracle implementation for generic JDBC Array interface.CallableStatement
– The interface used to execute SQL stored procedures.
References
Get our Articles via Email. Enter your email address.
This is great article
Hi this is sridhar, I executed the above program and stored procedure but I am getting the malformed sql 92 exception I am not getting aware of where the error is whether it is from java side or oracle zide could you please help me thanks for all
Thanks & regards
Sridhar
Hi Viral,
I couldn’t find any reply from you, please help me regarding the above issue.
Hi
I’m Try to execute the above code
i getting error while creating procedure:
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;
———————————
Error report:
ORA-01435: user does not exist
01435. 00000 – “user does not exist”
*Cause:
*Action:
Try to remove “SchemaName.” from the script, this is only a placeholder.
Very nice article, Vibhati.
However, for this case, I won’t extend the nested table by 10. It should either be extended by p_array.COUNT or each time by 1 inside the loop. It would avoid returning null values.
Hi Thanx,
nice post,
I have tried it but it is showing null or blank. it never show correct data.
Your example use array, can you give an example use ArrayList as input?
I’m afraid you cannot do that. You need to convert arraylist to array and then pass the array to stored procedure.
Hi Joe,
Object –
Array -
Java class.
Employee java class
.i am facing a problem. EmpName is showing blank in DB. but other field is woking fine.
Nice article,,,
Hi,
Is there any way to bind package level associative arrays in java code with hash map.
Please help.
I am trying to do the following.
create or replace
package hr_pkg
as
type charArray is table of varchar2(255) index by varchar2(10);
end;
–Java–
Map test = new HashMap();
test.put(“PLAN”, “2012″);
test.put(“ORDER”, “9999″);
ArrayDescriptor des = ArrayDescriptor.createDescriptor(“hr_pkg.charArray”, con);
ARRAY array_to_pass = new ARRAY(des,con,test);
When i execute this, i recieve
java.sql.SQLException: invalid name pattern: hr_pkg.charArray.
Any work around for this?
Thanks
Thanks for this post, I’m trying to make this work, but I’m not able to succeed
here is my example code – modified from your article:
oracle type definition is like this:
oracle proedure:
my problem:
application output looks like this:
database log table contains 8 new rows:
I really don’t understand – when I change String array to int[], (and database type to “table of number”), everything works just fine (log sql table contains A1, A2, A3, A4, B1, B2, B3, B4)… but it is impossible to for me to make it work with string
any ideas why is it so?
my oracle driver is ojdbc6-11.2.0.4.0, database version Oracle9i Enterprise Edition Release 9.2.0.8.0 – 64bit Production… every example I have found on the internet claims it works just fine – I really don’t understand
reply to my previous comment: including orai18n.jar should solve the issue – it helped in my case…
Hi Viral,
I have a requirement to pass a null Array to a stored procedure in java using callableStatement.setArray(int, ARRAY). Could you please let me know if it is possible?
- Prasad B
hi. i have a trouble when accessing the array in the stored procedure. i keep getting null values.
Any workaround on this bug. thank you