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