Skip to Main Content
  • Questions
  • Returning a Java Array into PL/SQL via a Java Stored Procedure and vice versa

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question, John.

Asked: October 31, 2000 - 1:06 am UTC

Last updated: October 18, 2008 - 8:27 pm UTC

Version: 8.1.7

Viewed 10K+ times! This question is

You Asked

Do you have any sample code to show how a Java Array can be passed through PL/SQL and how a PL/SQL Array or record could be passed to a Java Array via a Java Stored Procedure?

Thanks,

John Perkins

and Tom said...

Rating

  (8 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Arrays from pl/sql to java

Peter, August 06, 2002 - 8:30 am UTC

I think the question was about passing in, not using JDBC.

ie
myArray BigArrayType
begin
myJavaCall(myArray);
end;

Tom Kyte
August 07, 2002 - 9:03 am UTC

Question was:

a) how a java array can be passed to plsql. That is exactly the example at:
</code> http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:712625135727

b) how a plsql array could be passed to a java array via a java sp.  That is exactly the example at:
http://asktom.oracle.com/pls/asktom/f?p=100:11:::::P11_QUESTION_ID:331217279811 <code>

Now the fact is -- if you want java to talk to plsql you sort of NEED JDBC. That is part of the pie here.



how to pass string array to Java Stored Procedure.

vijaya, February 23, 2005 - 1:11 pm UTC

I would like to know.
how to pass string array to Java Stored Procedure.
Please try to help me in this regard.

Tom Kyte
February 24, 2005 - 5:04 am UTC

did you click on the links above in the original answer??

My JAVA function takes an array as an input parameter.

Andy Bay, October 17, 2008 - 9:35 am UTC

The link to http://asktom.oracle.com/pls/asktom/f?p=100:11:2103844855141813::::P11_QUESTION_ID:331217279811 does not discuss my problem.

...
public String myJavaFuncWithArrayIn(whatGoesHere p_array){
//How do I read p_array now that I'm here?
return "";
}
...


And just to tie it together, what goes in the SQL that marries this Java function with a PL/SQL function handle?

No actual database tables will be harmed (used) in the example.

Thanks,
Andy
Tom Kyte
October 18, 2008 - 8:27 pm UTC

... does not discuss my problem.
....

sorry?? I mean, given you didn't ask the question or anything?


but you know what, actually, I just read it.

If the question is

Ok, plsql has passed my java routine an array, how does java access that array, the link you say "doesn't discuss my problem" actually does. Tell me, what is the difference between an OUT parameter coming back from plsql and an input to a java routine - as far as mapping and accessing go??

anyway, short excerpt from Expert one on one Oracle - of some of the code. The plsql spec for passing collections:
tkyte@TKYTE816> create or replace type numArray as table of number;
Type created.

tkyte@TKYTE816> create or replace type dateArray as table of date;
Type created.

tkyte@TKYTE816> create or replace type strArray as table of varchar2(255);
Type created.


...
 22
 23      procedure pass( p_in in numArray, p_out out numArray )
 24      as language java
 25      name 'demo_passing_pkg.pass_num_array( oracle.sql.ARRAY,
 26                                             oracle.sql.ARRAY[] )';
 27
 28      procedure pass( p_in in dateArray, p_out out dateArray )
 29      as language java
 30      name 'demo_passing_pkg.pass_date_array( oracle.sql.ARRAY,
 31                                              oracle.sql.ARRAY[] )';
 32
 33
 34      procedure pass( p_in in strArray, p_out out strArray )
 35      as language java
 36      name 'demo_passing_pkg.pass_str_array( oracle.sql.ARRAY,
 37                                             oracle.sql.ARRAY[] )';
....



and the java stored procedures:


This next routine is a private (internal) routine.  It simply prints out meta-data about the oracle.sql.ARRAY that is passed to it.  Each of the three array types we send down to Java will make use of this routine just to report back what size/type they are:

 94  private static void show_array_info( oracle.sql.ARRAY p_in )
 95  throws SQLException
 96  {
 97      System.out.println( "Array is of type      " +
 98                           p_in.getSQLTypeName() );
 99      System.out.println( "Array is of type code " +
100                           p_in.getBaseType() );
101      System.out.println( "Array is of length    " +
102                           p_in.length() );
103  }
104

Now for the routines that manipulate the arrays.  Arrays are easy to use once you figure out how to get the data out of them and then back in.  Getting the data out is very easy -- the "getArray()" method will return the base data array for us. We simply need to cast the return value from getArray to the appropriate type and we then have a Java array of that type. Putting the data back into an array is a little more complex. We must first create a descriptor (meta-data) about the array and then create a new array object with that descriptor and the associated values. The following set of routines demonstrate this for each of the array types in turn.  Note that the code is virtually identical ¿ with the exception of the times we actually access the Java array of data.  All these routines do is show us the meta-data of the oracle.sql.ARRAY type, print out the contents of the array, and finally copy the input array to the output array:

105  public static void pass_num_array( oracle.sql.ARRAY p_in,
106                                     oracle.sql.ARRAY[] p_out )
107  throws SQLException
108  {
109      show_array_info( p_in );
110      java.math.BigDecimal[] values = (BigDecimal[])p_in.getArray();
111
112      for( int i = 0; i < p_in.length(); i++ )
113          System.out.println( "p_in["+i+"] = " + values[i].toString() );
114
115      Connection conn = new OracleDriver().defaultConnection();
116      ArrayDescriptor descriptor =
117         ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );
118
119      p_out[0] = new ARRAY( descriptor, conn, values );
120
121  }
122
123  public static void
124  pass_date_array( oracle.sql.ARRAY p_in, oracle.sql.ARRAY[] p_out )
125  throws SQLException
126  {
127      show_array_info( p_in );
128      java.sql.Timestamp[] values = (Timestamp[])p_in.getArray();
129
130      for( int i = 0; i < p_in.length(); i++ )
131          System.out.println( "p_in["+i+"] = " + values[i].toString() );
132
133      Connection conn = new OracleDriver().defaultConnection();
134      ArrayDescriptor descriptor =
135         ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );
136
137      p_out[0] = new ARRAY( descriptor, conn, values );
138
139  }
140
141  public static void
142  pass_str_array( oracle.sql.ARRAY p_in, oracle.sql.ARRAY[] p_out )
143  throws java.sql.SQLException,IOException
144  {
145      show_array_info( p_in );
146      String[] values = (String[])p_in.getArray();
147
148      for( int i = 0; i < p_in.length(); i++ )
149          System.out.println( "p_in["+i+"] = " + values[i] );
150
151      Connection conn = new OracleDriver().defaultConnection();
152      ArrayDescriptor descriptor =
153         ArrayDescriptor.createDescriptor( p_in.getSQLTypeName(), conn );
154
155      p_out[0] = new ARRAY( descriptor, conn, values );
156
157  }
158


Thank you!

Andy Bay, October 22, 2008 - 4:48 pm UTC

I'm sorry for being a rude. I'd been fighting a number of different ways to get something to work and finding examples that didn't match.

Although I did eventually find what I was looking for elsewhere, the answer is what I was looking for that matches what I interpreted the original question to read like.

Ultimately, here's what I came up with: http://forums.oracle.com/forums/thread.jspa?threadID=717861&tstart=150

Thanks,
Andy

Passing arrays from java to sql ...

Dave, November 04, 2008 - 2:26 pm UTC

A reader, April 27, 2011 - 8:04 am UTC


Passing an Array and Retrieving an array

Nithya, December 02, 2011 - 3:46 am UTC

The below example takes an array as an input and returns an array as output..Hope it might be usefull.

U can also refer the link of Tom
http://asktom.oracle.com/pls/asktom/f?p=100:11:355129109352988::::P11_QUESTION_ID:8908169959941


In java
-------
//DB call
Class.forName("oracle.jdbc.driver.OracleDriver");
// connect to oracle and login
String url = "jdbc:oracle:thin:@ .........";
Connection conn = DriverManager.getConnection(url,"username","password");

def descArray = new String[4]///0-3 so 4 size and 0-2 so 3 size
descArray[0]="Desc2"
descArray[1]="Desc3"
descArray[2]="Desc1"
descArray[3]="Desc4"

CallableStatement cstmt = conn.prepareCall("{call PKG_MATCH_REPORT.prc_calculate_tempTable(?,?)}");

ArrayDescriptor desc = ArrayDescriptor.createDescriptor("VC_ARRAY", conn);
ARRAY newArray = new ARRAY(desc, conn, descArray);

println newArray
cstmt.setArray(1, newArray);
cstmt.registerOutParameter(2,OracleTypes.ARRAY,"GFKP.VC_ARRAY");// OUTPUT ARRAY

cstmt.executeQuery();
oracle.sql.ARRAY dNames = cstmt.getArray(2);
println 'OutputArray->'+cstmt.getArray(2).getArray()
cstmt.close();



In PL/SQL
---------
CREATE or REPLACE TYPE VC_ARRAY AS TABLE OF VARCHAR2(40);
-----create Type at the schema level


and SP is as below:


PROCEDURE prc_calculate_tempTable
(
inparam IN vc_array,
outparam OUT vc_array
) AS
BEGIN

/* TODO implementation required */
---Add your implementation here
---For now i have tried just to return the input array

outparam:=inparam;
END prc_calculate_tempTable;

suri_123, July 26, 2012 - 6:50 am UTC

I want to pass an arry from java to pl/sql. but the array is ArrayList which consists of 10,000 elemets(String type).

sql:
------
CREATE OR REPLACE TYPE VICC AS VARRAY(10000) OF number(38);
/

java:
-------
ArrayDescriptor descriptor = ArrayDescriptor.createDescriptor("VICC", con);

ARRAY iccid=new ARRAY( descriptor, con, iccidSequence.toArray() );

while run the application i'm getting Internal Servler error at this line.

ARRAY iccid=new ARRAY( descriptor, con, iccidSequence.toArray() );

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library