I’m trying to create a simple example to understand how to store then retrieve (float array) data from/to a Java program to an Oracle database (11.2G) via a PL/SQL stored procedure based on VARRAY.
I’ve tried my best to put together the Java program, but there may be an error or two. I’m also stuck on how to write the PL/SQL code to write then read the VARRAY to/from the database. Any advice would be appreciated.
Java program:
// create example array
double[] myArray = new double[3];
myArray[0] = 1.1;
myArray[1] = 2.2;
myArray[2] = 3.3;
...
// setup call to stored procedure using SQL92 syntax
cs = conn.prepareCall( "{call my_sproc (?,?)}" );
// set IN parameters
cs.setString(1, myArray);
// set OUT parameters
cs.registerOutParameter(2, Types.ARRAY, "my_array");
// execute
cs.execute();
// retrieve array
double[] returnedArray = new double[3];
returnedArray = cs.getArray(2);
...
PL/SQL stored procedure:
create or replace procedure my_sproc (
input_array IN as VARRAY(3) of BINARY_FLOAT,
output_array OUT as VARRAY(3) of BINARY_FLOAT )
as
begin
-- how to write input_array into any example table?
-- how to read input_array from example table and store in variable: output_array?
end my_sproc;
It’s pretty uncommon in PL/SQL to use a
VARRAYsince you have to specify a maximum length. It’s much more common to use collections based on nested tables or associative arrays.If you do want to use
VARRAYbased collections, you can do something likeYou can call the procedure from PL/SQL
It would be much more common to declare and use a nested table type which ends up looking almost exactly the same as the
VARRAYcode just without the length limitIf you want to store the order of the elements of the array