Solved

How I get PLSQL table through java

Posted on 2004-09-09
8
1,388 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 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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
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 learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now