Search code examples
javaoracleibatisjdbc-odbc

from java how to call a stored procedure passing an oracle cursor as an argument


I am using Java + iBatis and have a need to call an Oracle Stored Procedure that takes a cursor as an argument. Google didn't help me much in finding a code sample of how to call a stored procedure that accepts a cursor as an argument from java.

How can this be accomplished?

Scenario in steps:

 1. Java calls a Stored Proc passing primitives (varchar, char, etc) as
    parameters 
 2. Java retrieves the cursor returned from Step 1 
 3. Java calls a Stored Proc passing cursor from Step 2 as an argument  //how? 

Solution

  • If those are really the only steps -- i.e. you aren't doing anything of importance in Java between the two calls -- then it makes more sense to me to avoid returning to Java at all.

    If the first procedure were actually a function, you could simply do a single nested call:

    BEGIN proc2(proc1(...)); END;
    

    The cursor gets passed within Oracle and never needs to be handled by Java at all.

    If your first procedure is a procedure that returns the cursor as an OUT parameter, you could write a wrapper function for it and do the same thing:

    CREATE OR REPLACE FUNCTION func1(...)
      RETURN SYS_REFCURSOR
      AS
        foo SYS_REFCURSOR;
      BEGIN
        proc1(..., foo);
        RETURN foo;
      END func1;
    /
    

    Then BEGIN proc2(func1(...)); END; should work.

    Now, if you really do need to go out to Java between the two calls, then I would try using OracleTypes.CURSOR when retrieving the output value from the first procedure, then simply pass that object into the second procedure. I don't know if this will work; if not, then there's probably no direct way to do it.