Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

How I get PLSQL table through java

Posted on 2004-09-09
8
Medium Priority
?
1,437 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
  • 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 252 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 248 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering 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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Suggested Courses

916 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