Link to home
Start Free TrialLog in
Avatar of harishkg
harishkg

asked on

How I get PLSQL table through java

How is it possible to call a procedure in oracle which returns a PL/SQL Table using CallableStatement

KG
Avatar of cjjclifford
cjjclifford

Connection conn = getJDBCConnection();
CallableStatement cstmt = conn.prepareCall( "{ ? = call my_func( ? ) }" );
cstmt.registerOutParameter( 1, Types.VARCHAR ); // assuming VARCHAR returned!
cstmt.setString( 2, "Hello world" );
cstmt.execute();
String returnval = cstmt.getString( 1 );
cstmt.close();
ASKER CERTIFIED SOLUTION
Avatar of cjjclifford
cjjclifford

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
grim_toaster's approach is what I had problems with before, with more complex objects (using the SQLType interface in Java), due to the actual type being owned by a different user than the connection's user (package accessed through synonym), which is why I needed to use STRUCT array, and inspect the types of attributes for each object being returned, and manually build the Java object to represent it.... a real pain!)
--> with more complex objects (using the SQLType interface in Java),
The SQLType interface???

Not had problems personally, provided the appropriate privileges have been granted to each user (directly), even when using arrays of Oracle objects (but yes, it is an absolute pain!!)  What problems did you have, I'm intrigued...
sorry, meant "SQLData" interface.

As I mentioned, I had problems when using a package that was owned by a different user (through a synonym) that was returning an array of objects which were also accessible through a synonym (their type was also owned by the other user). (The package was owned by a user that was created to have several DBA type roles, rather than granting those roles to every user (several instances of the application can be installed each with different normal schemas. Each instance would have a package created in this power user's schema (possibly different release versions of the product also, so different versions of package also - hence the synonym giving the package the same synonym name in each normal schema, but different real names (by version) in the "power" schema.

Now, the problem that was seen was when trying to extract the array of the Java implementation of these objects. Its been a while, but because it was quite a strange one, I actually put the Oracle error into a comment in the Java code!!!

java.sql.SQLException: ORA-21700: object does not exist or is marked for delete
ORA-06512: at "SYS.DBMS_PICKLER", line 15
ORA-06512: at "SYS.DBMS_PICKLER", line 52
ORA-06512: at line 1

is the actual error, as far as I could figure out it was due to trying to automatically rebuild objects that are actually synonyms to their real types...
Tried several different ways of addressing the type, and the package, with no avail. Eventually I got around this by fetching a STRUCT[] with the following:

ARRAY array = ((OracleResultSet)resultSet).getARRAY( 1 );
Object returnedArray = array.getArray();
STRUCT[] struts = null;
try {
    struct = (STRUCT[])Arrays.asList( (Object[]) returnedArray ).toArray( new STRUCT[0] );
}

and then going through the STRUCT[], confirming the attributes (correct # and types), and manually building the Java object.... Way more painful than the SQLData interface I'll say!

Cheers,
C.