JDBC: calling a stored procedure with multiple return values.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • randy.p.ho@gmail.com

    JDBC: calling a stored procedure with multiple return values.

    Using JDBC, is there a way to call a stored procedure with multiple
    return values? Thanks.

  • Joe Weinstein

    #2
    Re: JDBC: calling a stored procedure with multiple return values.



    randy.p.ho@gmai l.com wrote:
    [color=blue]
    > Using JDBC, is there a way to call a stored procedure with multiple
    > return values? Thanks.[/color]

    Absolutely. What do you mean by 'multiple return values'? Multiple output
    parameters? Multiple result sets and/or update counts? Multiple mixes of
    result sets and update counts?
    If you will show the procedure signature and maybe even the text? Tell
    us what the body of the procedure returns.

    Joe Weinstein at BEA

    Comment

    • randy.p.ho@gmail.com

      #3
      Re: JDBC: calling a stored procedure with multiple return values.

      Thanks for the reply. I meant "multiple output parameters".

      Here is how I execute the stored procedure:

      declare @ErrorID int
      declare @ErrorStr varchar(255)
      exec procName
      @customerId = '1234567890',
      @customerName = 'some name',
      @error_code = @ErrorID,
      @error_state = @ErrorStr


      Here is the procedure:

      create procedure uxt1.procName
      @customerId char(15) output,
      @customerName char(64) output,
      @error_code int output,
      @error_state varchar(255) output
      .... ...
      /* all the business logic */
      .... ...
      return (@error_state)
      GO


      Here's what SQL server gives me if I do a "Script object as Execute":
      DECLARE @RC int
      DECLARE @customerId char(15)
      DECLARE @customerName char(64)
      DECLARE @error_code int
      DECLARE @error_state varchar(255)
      EXEC @RC = [uxt1].[procName] @customerId, @customerName, @error_code
      OUTPUT , @error_state OUTPUT


      The following is what I've tried in a Java program:
      ....
      CallableStateme nt cs = conn.prepareCal l(" {? = call
      uxt1.procName(? ,?,?,?)}" );
      cs.registerOutP arameter(1,java .sql.Types.INTE GER);
      cs.setString(2, "some ID");
      cs.setString(3, "some Name");
      cs.registerOutP arameter(4,java .sql.Types.INTE GER);
      cs.registerOutP arameter(5,java .sql.Types.VARC HAR);
      ResultSet rs = cs.executeQuery ();
      ....

      My code doesn't throw any exception; but the procedure was not executed
      correctly (i.e. it's not doing what it's supposed to do, which is to
      simply insert some values into a table).
      Any help is appreciated. Thanks in advance.

      Comment

      • Joe Weinstein

        #4
        Re: JDBC: calling a stored procedure with multiple return values.



        randy.p.ho@gmai l.com wrote:
        [color=blue]
        > Thanks for the reply. I meant "multiple output parameters".
        >
        > Here is how I execute the stored procedure:
        >
        > declare @ErrorID int
        > declare @ErrorStr varchar(255)
        > exec procName
        > @customerId = '1234567890',
        > @customerName = 'some name',
        > @error_code = @ErrorID,
        > @error_state = @ErrorStr
        >
        >
        > Here is the procedure:
        >
        > create procedure uxt1.procName
        > @customerId char(15) output,
        > @customerName char(64) output,
        > @error_code int output,
        > @error_state varchar(255) output
        > ... ...
        > /* all the business logic */
        > ... ...
        > return (@error_state)
        > GO
        >
        >
        > Here's what SQL server gives me if I do a "Script object as Execute":
        > DECLARE @RC int
        > DECLARE @customerId char(15)
        > DECLARE @customerName char(64)
        > DECLARE @error_code int
        > DECLARE @error_state varchar(255)
        > EXEC @RC = [uxt1].[procName] @customerId, @customerName, @error_code
        > OUTPUT , @error_state OUTPUT
        >
        >
        > The following is what I've tried in a Java program:
        > ...
        > CallableStateme nt cs = conn.prepareCal l(" {? = call
        > uxt1.procName(? ,?,?,?)}" );
        > cs.registerOutP arameter(1,java .sql.Types.INTE GER);
        > cs.setString(2, "some ID");
        > cs.setString(3, "some Name");
        > cs.registerOutP arameter(4,java .sql.Types.INTE GER);
        > cs.registerOutP arameter(5,java .sql.Types.VARC HAR);
        > ResultSet rs = cs.executeQuery ();
        > ...
        >
        > My code doesn't throw any exception; but the procedure was not executed
        > correctly (i.e. it's not doing what it's supposed to do, which is to
        > simply insert some values into a table).
        > Any help is appreciated. Thanks in advance.[/color]

        Is the procedure executing at all? You shouldn't be calling executeQuery()
        unless the first thing the procedure does is a select. Use execute() and
        then loop:

        cs.execute();
        while (true)
        {
        int update_count = ps.getUpdateCou nt();
        ResultSet rs = ps.getResultSet ();
        if ((rs == null && (update_count == -1)) break; // done

        if (rs != null) process rs;
        ps.getMoreResul ts();
        }
        // after processing inline results, call ps.getXXX() to get output parameters.

        Whatever jdbc driver you're suing is pretty flakey if it
        returns a result set from executeQuery() and the
        procedure didn't do a select for data to go to the caller...

        Joe Weinstein at BEA


        Comment

        • jogri13
          New Member
          • May 2006
          • 1

          #5
          I have closely the same problem, excepting that my stored procs returns a "TABLE".

          In fact, I have a SQL function that must return a TABLE and get 2 parameters. Its signature is as follow:

          FUNCTION myFunction (@codett varchar(40), @codet varchar(40) )
          RETURNS @Tab TABLE([cp] [varchar] (40), [pourcentage] [int])

          I'd like to call it from my DAO (using JDBC). having that 'cs' is a CallableStateme nt, I tried:

          cs = jdbcConnection. prepareCall("{? = call myFunction(?, ?)}");
          cs.registerOutP arameter( 1, Types.OTHER );
          cs.setString(2, codett);//codett contains a String
          cs.setString(3, codet);//codet contains a String
          ResultSet rset = cs.executeQuery ();

          However, when a get to the "executeQuery() " method, I get an SQLException saying that "myFunction " is a function Object.

          have you met such a problem ?

          Comment

          Working...