Solved

How I get PLSQL table through java

Posted on 2004-09-09
8
1,405 Views
Last Modified: 2008-01-09
How is it possible to call a procedure in oracle which returns a PL/SQL Table using CallableStatement

KG
0
Comment
Question by:harishkg
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
8 Comments
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12014613
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();
0
 
LVL 11

Accepted Solution

by:
cjjclifford earned 63 total points
ID: 12014635
sorry, missed the bit about the PLSQL table...

Perhaps cast the function call, and just use SQL:

SELECT * FROM TABLE( my_func() );

Or you can use the Array of Object mechanism, using the SQLType interface (I've had problems with this!)

Failing that, you will probably have to use STRUCT array (I've had problems before using arrays of custom objects being returned from PL/SQL procedures, but that was because the objects being returned were of types owned by a different user in the database....)
0
 
LVL 7

Assisted Solution

by:grim_toaster
grim_toaster earned 62 total points
ID: 12015744
Depending on what you want to do with the data, it may be worthwhile to create using the table command as previously defined.  Alternatively, here's a step-by-step guide to actually return an Array:

The PL/SQL code required to set up the example:
CREATE OR REPLACE TYPE table_type IS TABLE OF VARCHAR2(8);
CREATE OR REPLACE FUNCTION get_test_array RETURN table_type AS
BEGIN
    RETURN table_type('hello', 'from', 'the', 'array');
END;

And the java code:
            cstmt = conn.prepareCall("{ ? = call get_test_array() }");
            cstmt.registerOutParameter(1, Types.ARRAY, "TABLE_TYPE");
            cstmt.execute();
            Array array =  cstmt.getArray(1);

            String[] values = (String[]) array.getArray();
            for (int i = 0; i < values.length; i++) {
                System.out.println("row " + i + " = '" + values[i] + "'");
            }
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Expert Comment

by:cjjclifford
ID: 12015867
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!)
0
 
LVL 7

Expert Comment

by:grim_toaster
ID: 12016674
--> 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...
0
 
LVL 11

Expert Comment

by:cjjclifford
ID: 12016999
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.
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
This video teaches viewers about errors in exception handling.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question