Home » Developer & Programmer » JDeveloper, Java & XML » How to pass an array from Java which maps to a PL/SQL table in oracle?
How to pass an array from Java which maps to a PL/SQL table in oracle? [message #91270] Tue, 25 June 2002 12:04 Go to next message
Devang Pandya
Messages: 9
Registered: June 2002
Junior Member
How to pass an array from Java which maps to a PL/SQL table in oracle?

e.g.

I'm using a stored procedure in the following way..

rules_engine (p_input_array IN DBMS_SQL.varchar2_table,
p_output_array OUT DBMS_SQL.varchar2_table) ;

I want to map an array from java to p_input_array variable.

Plz. help me its very urgent...Thanx in advance
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #91271 is a reply to message #91270] Tue, 25 June 2002 12:30 Go to previous messageGo to next message
Mahesh Rajendran
Messages: 10708
Registered: March 2002
Location: oracleDocoVille
Senior Member
Account Moderator
This may be useful to you.
import java.sql.*;
import java.io.*;
import java.util.*;
import java.math.BigDecimal;
import oracle.sql.*;
import oracle.jdbc.driver.*;

public class callInOutStructArray extends Object {

/*
   This program demonstrates how to emulate calling a stored procedure
   with PL/SQL table of record parameters via JDBC.  You cannot call a 
   PL/SQL table of records parameter directly, however, you can use an 
   Object with the same structure as your table of records.

   The Script used to create the procedure in this example is as follows:

   drop type rectab;

   create or replace type rectype as object(col1 varchar2(10),col2 varchar2(10));
   /

   create or replace type rectab as table of rectype;
   /

   create or replace package ioStructArray as

   procedure testproc(iorec in out rectab,orec out rectab);

   end ioStructArray;
   /

   create or replace package body ioStructArray as
 
   procedure testproc(iorec in out rectab,orec out rectab) is
   begin
      orec := iorec;
      for i in 1..iorec.count loop
         iorec(i).col1 := orec(i).col2;
         iorec(i).col2 := orec(i).col1;
      end loop;
   end testproc;
   end ioStructArray;
   /

*/
   public static void main(String[] args) throws SQLException {

      DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());

    // Connect to the database

      Connection conn =
         DriverManager.getConnection ("jdbc:oracle:oci8:@S692815.WORLD",
                                   "scott", "tiger");

   //  First, declare the Object arrays that will store the data.

      Object [] p1recobj = {"FIRST","LAST"};
      Object [] p2recobj = {"SECOND","LAST"};
      Object [] p3recobj;
      Object [] p4recobj;

   //  Declare the Object Arrays to hold the STRUCTS.

      Object [] p1arrobj;
      Object [] p2arrobj;

   //  Declare two descriptors, one for the ARRAY TYPE
   //  and one for the OBJECT TYPE.

      StructDescriptor desc1=StructDescriptor.createDescriptor("RECTYPE",conn);
      ArrayDescriptor desc2=ArrayDescriptor.createDescriptor("RECTAB",conn);

   //  Create the STRUCT objects to associate the host objects
   //  with the database records.

      STRUCT p1struct = new STRUCT(desc1,conn,p1recobj);
      STRUCT p2struct = new STRUCT(desc1,conn,p2recobj);

   //  Initialize the Input array object - to an array of STRUCT Objects.

      p1arrobj = new Object []{p1struct,p2struct};

   //  Set up the ARRAY object.

      ARRAY  p1arr = new ARRAY(desc2,conn,p1arrobj);
      ARRAY  p2arr;

   //  Declare the callable statement.
   //  This must be of type OracleCallableStatement.

      OracleCallableStatement ocs =
         (OracleCallableStatement)conn.prepareCall("{call iostructarray.testproc(?,?)}");

   //  The first parameter is in out so we have to use setARRAY to
   //  pass it to the statement

      ocs.setARRAY(1,p1arr);

   //  The first parameter is in out, so we have to Register the 
   //  parameter as well.
   //  Note the re use of the TYPE.

      ocs.registerOutParameter(1,OracleTypes.ARRAY,"RECTAB");

  //   The second paramter is out, so that has to be registered too.
  //   Note the re use of the TYPE.

      ocs.registerOutParameter(2,OracleTypes.ARRAY,"RECTAB");

   //  Execute the procedure

      ocs.execute();

   //  Associate the returned arrays with the ARRAY objects.

      p1arr = ocs.getARRAY(1);
      p2arr = ocs.getARRAY(2);

   //  Get the data back into the data arrays.

      p1arrobj = (Object [])p1arr.getArray();

      p2arrobj = (Object [])p2arr.getArray();

   //  Get the data records from each array element (which is of type STRUCT).

      p1recobj = ((STRUCT)p1arrobj[0]).getAttributes();
      p2recobj = ((STRUCT)p1arrobj[1]).getAttributes();

      p3recobj = ((STRUCT)p2arrobj[0]).getAttributes();
      p4recobj = ((STRUCT)p2arrobj[1]).getAttributes();

   //  Show the results:

      System.out.println("First Object is now "+p1recobj[0]+" and "+p1recobj[1]);
      System.out.println("                    "+p2recobj[0]+" and "+p2recobj[1]);
      System.out.println("Second Object is now "+p3recobj[0]+" and "+p3recobj[1]);
      System.out.println("                    "+p4recobj[0]+" and "+p4recobj[1]);

   }
}
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #91273 is a reply to message #91270] Wed, 26 June 2002 13:21 Go to previous messageGo to next message
Devang Pandya
Messages: 9
Registered: June 2002
Junior Member
Thanx Mahesh Rajendran... I've got it done...
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #91793 is a reply to message #91270] Thu, 20 February 2003 05:15 Go to previous messageGo to next message
Sorin Rosu
Messages: 1
Registered: February 2003
Junior Member
I used this code and
I get a invalid name pattern exception
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92243 is a reply to message #91271] Tue, 04 May 2004 07:32 Go to previous messageGo to next message
Rakesh Kumar Kunwar
Messages: 1
Registered: May 2004
Junior Member
Hi.

This example look cool. It really helped me. But I have different case. I have store procedure which accept table of record as parameter in place of table of object. So if some body can help me how to pass talble of record to oracle store procedure from java.
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92272 is a reply to message #91271] Thu, 13 May 2004 12:34 Go to previous messageGo to next message
DB
Messages: 7
Registered: February 2003
Junior Member
Hi,

I am getting a ClassCastException on

OracleCallableStatement ocs = (OracleCallableStatement)conn.prepareCall("{call iostructarray.testproc(?,?)}");

please advise

thanks,
db
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92450 is a reply to message #91271] Wed, 21 July 2004 23:34 Go to previous messageGo to next message
Fabian
Messages: 19
Registered: October 2000
Junior Member
It does not work with a connection pool. It gives me a ClassCastException.
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92451 is a reply to message #92450] Wed, 21 July 2004 23:50 Go to previous messageGo to next message
Fabian
Messages: 19
Registered: October 2000
Junior Member
I've solved the ClassCastException doing this:

conexion1 = getConexion();

conexion = ((oracle.jdbc.driver.OracleConnection)((com.ibm.ejs.cm.proxy.OracleConnectionProxy)conexion1).getPhysicalConnection());
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92452 is a reply to message #92272] Wed, 21 July 2004 23:56 Go to previous messageGo to next message
Fabian
Messages: 19
Registered: October 2000
Junior Member
do this:

((oracle.jdbc.driver.OracleConnection)((com.ibm.ejs.cm.proxy.OracleConnectionProxy)conn).getPhysicalConnection());
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92456 is a reply to message #91271] Sat, 24 July 2004 08:59 Go to previous messageGo to next message
Nitin
Messages: 35
Registered: July 1999
Member
It is working fine with the classes12.jar but now we are using new jdbc driver oracle provided ojdbc14.jar and this procedure is not working with this driver. Will you provide me any suggestions. Following is the error messages I am getting-

[[java]] 13:02:02,672 ERROR [[STDERR]] java.sql.SQLException: Internal Error: I
nconsistent catalog view
[[java]] 13:02:02,672 ERROR [[STDERR]] at oracle.jdbc.dbaccess.DBError.
throwSqlException(DBError.java:134)
[[java]] 13:02:02,672 ERROR [[STDERR]] at oracle.jdbc.dbaccess.DBError.
throwSqlException(DBError.java:179)
[[java]] 13:02:02,672 ERROR [[STDERR]] at oracle.sql.StructDescriptor.i
nitMetaData1_9_0(StructDescriptor.java:1838)
[[java]] 13:02:02,672 ERROR [[STDERR]] at oracle.sql.StructDescriptor.i
nitMetaData1(StructDescriptor.java:1782)
[[java]] 13:02:02,672 ERROR [[STDERR]] at oracle.sql.StructDescriptor.i
sInstantiable(StructDescriptor.java:1027)
[[java]] 13:02:02,682 ERROR [[STDERR]] at oracle.sql.STRUCT.<init>(STRU
CT.java:126)
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92457 is a reply to message #92456] Sat, 24 July 2004 09:59 Go to previous messageGo to next message
Nitin
Messages: 35
Registered: July 1999
Member
The error I posted was not related to jdbc driver. Here is the actual error I am getting of using new driver. Please help me.

[[java]] 14:01:41,097 ERROR [[STDERR]] java.lang.NullPointerException
[[java]] 14:01:41,097 ERROR [[STDERR]] at oracle.jdbc.driver.OracleConn
ection.safelyGetClassForName(OracleConnection.java:5074)
[[java]] 14:01:41,097 ERROR [[STDERR]] at oracle.jdbc.driver.OracleConn
ection.addClassMapEntry(OracleConnection.java:2852)
[[java]] 14:01:41,097 ERROR [[STDERR]] at oracle.jdbc.driver.OracleConn
ection.addDefaultClassMapEntriesTo(OracleConnection.java:2843)
[[java]] 14:01:41,097 ERROR [[STDERR]] at oracle.jdbc.driver.OracleConn
ection.initializeClassMap(OracleConnection.java:2529)
[[java]] 14:01:41,107 ERROR [[STDERR]] at oracle.jdbc.driver.OracleConn
ection.ensureClassMapExists(OracleConnection.java:2523)
[[java]] 14:01:41,107 ERROR [[STDERR]] at oracle.jdbc.driver.OracleConn
ection.getTypeMap(OracleConnection.java:2829)
[[java]] 14:01:41,107 ERROR [[STDERR]] at oracle.sql.ARRAY.getMap(ARRAY
.java:863)
[[java]] 14:01:41,107 ERROR [[STDERR]] at oracle.sql.ARRAY.getArray(ARR
AY.java:370)
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92620 is a reply to message #92457] Thu, 21 October 2004 02:18 Go to previous messageGo to next message
Subahan
Messages: 2
Registered: October 2004
Junior Member
If any one can update on this Plz reply back
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #92626 is a reply to message #92620] Mon, 25 October 2004 21:37 Go to previous messageGo to next message
Subahan
Messages: 2
Registered: October 2004
Junior Member
I resolved the problem. The problem was we were closing the connection before calling getArray() method.
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #152084 is a reply to message #92243] Tue, 20 December 2005 22:30 Go to previous messageGo to next message
finale002
Messages: 2
Registered: December 2005
Junior Member
>Hi.
>
>This example look cool. It really helped me. But I have >different case. I have store procedure which accept table of >record as parameter in place of table of object. So if some >body can help me how to pass talble of record to oracle store >procedure from java.

I also meet the same problem:

In oracle, I defined the following types:
create or replace type REQ_RFDL4CHK_ITEM IS OBJECT
(
M_SYS_ID number,
M_NAME varchar2(255)
)
and
CREATE OR REPLACE TYPE "REQ_CHECK_LIST" AS TABLE OF "ZHANGTAO"."REQ_RFDL4CHK_ITEM"

and in the java source:
...
Vector objList = vobjItr.getVector();
Object[] objRFDList = new Object[objList.size()];
oracle.sql.StructDescriptor objStuctDesc;
for (int nCnt = 0; nCnt < objList.size(); nCnt ++){
CRFListItem objRFD = (CRFListItem)objList.get(nCnt);
long lngSysID = objRFD.getMlngSysID();
String strChecker = objRFD.getPropValue("CRFListItem.CHECKER").toString();
Object[] objRFDI = new Object[]{new BigDecimal(lngSysID), strChecker};

objStuctDesc =oracle.sql.StructDescriptor.createDescriptor("REQ_RFDL4CHK_ITEM", this.getDBConnection());
oracle.sql.STRUCT objDBRFDLI =new oracle.sql.STRUCT(objStuctDesc, this.getDBConnection(), objRFDI);

objRFDList[nCnt] = objDBRFDLI;
}
objStuctDesc = oracle.sql.StructDescriptor.createDescriptor("REQ_CHECK_LIST", this.getDBConnection());
oracle.sql.STRUCT objCheckList =
new oracle.sql.STRUCT(objStuctDesc, this.getDBConnection(), objRFDList);

...


The problem is: there is a exception occurred while the program excuted on the last stentance:"STRUCT objCheckList =
STRUCT(objStuctDesc, this.getDBConnection(), objRFDList);", and the exception is:
"... can not resolve to the name".

So, who can help me to point out where I am go wrong?
Thanks. Smile
Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #152086 is a reply to message #152084] Wed, 21 December 2005 00:14 Go to previous message
finale002
Messages: 2
Registered: December 2005
Junior Member
the problem has been resolved according to Mahesh Rajendran's message within the same topic:
"Re: How to pass an array from Java which maps to a PL/SQL table in oracle? [message #91271]"

thnx.
Previous Topic: ORA-00020 maximum number of processes exceeded
Next Topic: XML error
Goto Forum:
  


Current Time: Mon Nov 25 00:28:25 CST 2024