Solved

How I get PLSQL table through java

Posted on 2004-09-09
8
1,399 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
thymeleaf natural templating vs JSP 2 105
Basic Java Case or If-Else statement... 3 51
Why my table column Id is not passed to java object? 4 39
hibernate jars 4 32
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
The viewer will learn how to implement Singleton Design Pattern in Java.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

831 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