views:

37

answers:

1

I need to pass an array of objects to a PL-SQL function

My ORACLE code:

CREATE OR REPLACE
TYPE
        uu.ITEMTAB AS TABLE OF ITEMREC; 
/

CREATE OR REPLACE
TYPE
        uu.ITEMREC AS OBJECT (ID NUMBER,
                               NAME VARCHAR2(30)) 
/

Java class

public class ITEMREC {

    private int ID;
    private String NAME;

    public ITEMREC(int iD, String nAME) {
        super();
        ID = iD;
        NAME = nAME;
    }
    public int getID() {
        return ID;
    }
    public void setID(int iD) {
        ID = iD;
    }
    public String getNAME() {
        return NAME;
    }
    public void setNAME(String nAME) {
        NAME = nAME;
    }
}

Java code:

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

import oracle.sql.ARRAY;
import oracle.sql.ArrayDescriptor;

public class Testna {

    public static void main(String args[]) throws java.io.IOException, SQLException, ClassNotFoundException
    {

        ITEMREC[] myA = new ITEMREC[1];
        myA[0] = new ITEMREC(1, "BOB");

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con=DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:ORCL","uu","uu");

        ArrayDescriptor desc= ArrayDescriptor.createDescriptor("ITEMTAB", con);  
        ARRAY array = new ARRAY(desc, con, myA);

        System.out.println("Connection created..............");
        String call = "{ ? = call mainpackage.fgetText(?) }";
        CallableStatement cstmt = con.prepareCall(call);
        cstmt.setQueryTimeout(1800);
        cstmt.registerOutParameter(1, Types.VARCHAR);
        cstmt.setArray(2, array);
        cstmt.executeUpdate();
        String val = cstmt.getString(1);
        cstmt.close();
        con.close();
        System.out.println(val);
    }   
}

Note: here I have only one object, as this is just for test.

The new error I get is at:

ARRAY array = new ARRAY(desc, con, myA);

Exception in thread "main" java.sql.SQLException: Fail to convert to internal representation:

UPDATED: I have updated my code,

Thanx @Codo

+2  A: 

It's slightly more complicated than that. First, you need an array type in Oracle. It must be global, i.e. not defined with an PL/SQL package.

create or replace type NUM_ARRAY as table of number;

Then you need to create an Oracle specific array in Java:

int intArray[] = { 1,2,3,4,5,6 };

ArrayDescriptor desc= ArrayDescriptor.createDescriptor("NUM_ARRAY", con);  
ARRAY array = new ARRAY(desc, con, intArray);

This can now be passed to the stored procedure:

OraclePreparedStatement stmt =
  (OraclePreparedStatement)conn.prepareStatement("begin pkg.proc(:x); end;");
ps.setARRAY( 1, array_to_pass );  
ps.execute();

I'm not quite sure how important it is to use the Oracle specific classes from the oracle.jdbc package. But it's certainly not possible to work with pure JDBC.

If you provide more information about your array type (both on the Java and the Oracle side) as well as the signature of the PL/SQL procedure, I could give you more specific advice.

Updated:

Obviously, you're not trying to pass an array but a table. I've never done that and I don't know whether it's possible or not.

Codo
thanx Codo, I have just updated my code. Any ideas now?
Adnan
Condo, I have managed to do it. I had to use STRUCT to first describe the Oracle object.
Adnan