How to pass an array from Java which maps to a PL/SQL table in oracle? [message #91270] |
Tue, 25 June 2002 12:04 |
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 |
|
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 #92456 is a reply to message #91271] |
Sat, 24 July 2004 08:59 |
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 |
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 #152084 is a reply to message #92243] |
Tue, 20 December 2005 22:30 |
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.
|
|
|
|