Home | Previous Page | Next Page   Performing Database Operations > Parameters, Escape Syntax, and Unsupported Methods >

Using CallableStatement OUT Parameters

CallableStatement methods handle OUT parameters in C function and Java user-defined routines (UDRs). Two registerOutParameter() methods specify the data type of OUT parameters to the driver. A series of getXXX() methods retrieves OUT parameters.

IBM Informix Dynamic Server, Version 9.2x and 9.3x, considers OUT parameters to be statement local variables (SLVs). SLVs are valid only for the life of a single statement and cannot be returned directly upon executing the routine. The JDBC CallableStatement interface provides a method for retrieving OUT parameters.

With IBM Informix Dynamic Server, Version 10.0 and later, the OUT parameter routine makes available a valid blob descriptor and data to the JDBC client for a BINARY OUT parameter. Using receive methods in IBM Informix JDBC Driver, Version 3.0 and later, supporting IDS 10.0 and later, you can use these OUT parameter descriptors and data provided by the server.

Exchange of descriptor and data between IDS and JDBC is consistent with the existing mechanism by which data is exchanged for the result set methods of JDBC, such as passing the blob descriptor and data through SQLI protocol methods. (SPL UDRs are the only type of UDRs supporting BINARY OUT parameters.)

For background information, refer to the following documentation:

Only Informix database servers versions 9.2 and later return an OUT parameter to IBM Informix JDBC Driver. IBM Informix Dynamic Server, Version 9.4 and later supports multiple OUT parameters.

For examples of how to use OUT parameters, see the CallOut1.java, CallOut2.java, CallOut3.java, and CallOut4.java example programs in the basic subdirectory of the demo directory where your IBM Informix JDBC Driver is installed.

Server and Driver Restrictions and Limitations

Server Restrictions

This section describes the restrictions imposed by different versions of the 9.x and later Dynamic Server. It also describes enhancements made to the JDBC Driver and the restrictions imposed by it.

Versions 9.2x and 9.3x of IBM Informix Dynamic Server have the following requirements and limitations concerning OUT parameters:

These restrictions, for server versions 9.2x and 9.3x, are imposed whether users create C, SPL, or Java UDRs.

The functionality of the IBM Informix Dynamic Server, Version 9.4 allows:

You cannot specify INOUT parameters.

For more information on UDRs, see IBM Informix: User-Defined Routines and Data Types Developer's Guide and IBM Informix: J/Foundation Developer's Guide.

Driver Enhancement

The CallableStatement object provides a way to call or execute UDRs in a standard way for all database servers. Results from the execution of these UDRs are returned as a result set or as an OUT parameter.

The following is a program that creates a user-defined function, myudr, with two OUT parameters and one IN parameter, and then executes the myudr() function. The example requires server-side support for multiple OUT parameters; hence it will only work for IBM Informix Dynamic Server, Version 9.4 or above. For more information on UDRs, see IBM Informix: User-Defined Routines and Data Types Developer's Guide and IBM Informix: J/Foundation Developer's Guide.

import java.sql.*;
public class myudr {

  public myudr() {
  }

  public static void main(String args[]) {
    Connection myConn = null;
    try {
      Class.forName("com.informix.jdbc.IfxDriver");
      myConn = DriverManager.getConnection(
          "jdbc:informix-sqli:MYSYSTEM:18551/testDB:"
          +"INFORMIXSERVER=patriot1;user=USERID;"
          +"password=MYPASSWORD");
    }
    catch (ClassNotFoundException e) {
      System.out.println(
       "problem with loading Ifx Driver\n" + e.getMessage());
    }
    catch (SQLException e) {
      System.out.println(
        "problem with connecting to db\n" + e.getMessage());
    }
    try {
      Statement stmt = myConn.createStatement();
      stmt.execute("DROP FUNCTION myudr");
    }
    catch (SQLException e){
    }
    try
    {
      Statement stmt = myConn.createStatement();

      stmt.execute(
        "CREATE FUNCTION myudr(OUT arg1 int, arg2 int, OUT arg3 int)"
        +" RETURNS boolean; LET arg1 = arg2; LET arg3 = arg2 * 2;"
        +"RETURN 't'; END FUNCTION;");
    }
    catch (SQLException e) {
      System.out.println(
        "problem with creating function\n" + e.getMessage());
    }

    Connection conn = myConn;

    try
    {
      String command = "{? = call myudr(?, ?, ?)}";
      CallableStatement cstmt = conn.prepareCall (command);

      // Register arg1 OUT parameter
      cstmt.registerOutParameter(1, Types.INTEGER);

      // Pass in value for IN parameter
      cstmt.setInt(2, 4);

      // Register arg3 OUT parameter
      cstmt.registerOutParameter(3, Types.INTEGER);

      // Execute myudr
      ResultSet rs = cstmt.executeQuery();

      // executeQuery returns values via a resultSet
      while (rs.next())
      {
        // get value returned by myudr
        boolean b = rs.getBoolean(1);
        System.out.println("return value from myudr = " + b);
      }

      // Retrieve OUT parameters from myudr
      int i = cstmt.getInt(1);
      System.out.println("arg1 OUT parameter value = " + i);

      int k = cstmt.getInt(3);
      System.out.println("arg3 OUT parameter value = " + k);

      rs.close();
      cstmt.close();
      conn.close();
    }
    catch (SQLException e)
    {
      System.out.println("SQLException: " + e.getMessage());
      System.out.println("ErrorCode: " + e.getErrorCode());
      e.printStackTrace();
    }
  }
}
- - -
.../j2sdk1.4.0/bin/java ... myudr 
return value from myudr = true
arg1 OUT parameter value = 4
arg3 OUT parameter value = 8
Driver Restrictions and Limitations

IBM Informix JDBC Driver has the following requirements and limitations concerning OUT parameters:

These restrictions apply to a JDBC application that handles C, SPL, or Java UDRs.

IN and OUT Parameter Type Mapping

An exception is thrown by the registerOutParameter(int, int), registerOutParameter(int, int, int), or setNull(int, int) method if the driver cannot find a matching Informix type or finds a mapping ambiguity (more than one matching Informix type). The table that follows shows the mappings the CallableStatement interface uses. Asterisks ( * ) indicate mapping ambiguities.

java.sql.Types
com.informix.lang.IfxTypes
Array*
IFX_TYPE_LIST

IFX_TYPE_MULTISET

IFX_TYPE_SET

Bigint
IFX_TYPE_INT8
Binary
IFX_TYPE_BYTE
Bit
Not supported
Blob
IFX_TYPE_BLOB
Char
IFX_TYPE_CHAR (n)
Clob
IFX_TYPE_CLOB
Date
IFX_TYPE_DATE
Decimal
IFX_TYPE_DECIMAL
Distinct*
Depends on base type
Double
IFX_TYPE_FLOAT
Float
IFX_TYPE_FLOAT1
Integer
IFX_TYPE_INT
Java_Object*
IFX_TYPE_UDTVAR

IFX_TYPE_UDTFIX

Longvarbinary*
IFX_TYPE_BYTE

IFX_TYPE_BLOB

Longvarchar*
IFX_TYPE_TEXT

IFX_TYPE_CLOB

IFX_TYPE_LVARCHAR

Null
Not supported
Numeric
IFX_TYPE_DECMIAL
Other
Not supported
Real
IFX_TYPE_SMFLOAT
Ref
Not supported
Smallint
IFX_TYPE_SMINT
Struct
IFX_TYPE_ROW
Time
IFX_TYPE_DTIME (hour to second)
Timestamp
IFX_TYPE_DTIME (year to fraction(5))
Tinyint
IFX_TYPE_SMINT
Varbinary
IFX_TYPE_BYTE
Varchar
IFX_TYPE_VCHAR (n)
Nothing*
IFX_TYPE_BOOL

1 This mapping is JDBC compliant. You can map the JDBC FLOAT data type to the Informix SMALLFLOAT data type for backward compatibility by setting the IFX_SET_FLOAT_AS_SMFLOAT connection property to 1.

To avoid mapping ambiguities, use the following extensions to CallableStatement, defined in the IfmxCallableStatement interface:

public void IfxRegisterOutParameter(int parameterIndex, 
   int ifxType) throws SQLException;

public void IfxRegisterOutParameter(int parameterIndex, 
   int ifxType, String name) throws SQLException;

public void IfxRegisterOutParameter(int parameterIndex, 
   int ifxType, int scale) throws SQLException;
public void IfxSetNull(int i, int ifxType) throws SQLException;

public void IfxSetNull(int i, int ifxType, String name) throws
   SQLException;

Possible values for the ifxType parameter are listed in Using the IfxTypes Class.

IBM Informix Dynamic Server, Version 10.0, makes available to the JDBC client valid BLOB descriptors and data to support binary OUT parameters for SPL UDRs.

IBM Informix JDBC Driver, Version 3.0, can receive the OUT parameter descriptor and data provided by the server and use it in Java applications.

Note:
The single correct return value for any JDBC binary type (BINARY, VARBINARY, LONGVARBINARY) retrieved via method getParameterType (ParameterMetaData) is -4, which is associated with java.sql.Type.LONGVARBINARY data type. This reflects the fact that all the JDBC binary types are mapped to the same Informix SQL data type, BYTE.
Home | [ Top of Page | Previous Page | Next Page | Contents | Index ]