apache > db
Apache DB Project
 
Font size:      

CallableStatements and INOUT Parameters

CallableStatements and INOUT Parameters

INOUT parameters map to an array of the parameter type in Java. (The method must take an array as its parameter.) This conforms to the recommendations of the SQL standard.

Given the following example:

CallableStatement call = conn.prepareCall(
    "{CALL doubleMyInt(?)}");
 // for inout parameters, it is good practice to
// register the outparameter before setting the input value 
call.registerOutParameter(1, INTEGER);
call.setInt(1,10);
call.executeQuery();
int retval = call.getInt(1);

The method doubleIt should take a one-dimensional array of ints. Here is sample source code for that method:

public static void doubleMyInt(int[] i) {
    i[0] *=2;
     /* Derby  returns the first element of the array.*/ 
}

Note:
The return value is not wrapped in an array even though the parameter to the method is.

Table 11. INOUT Parameter Type Correspondence

JDBC TypeArray Type for Method ParameterValue and Return Type
BIGINTlong[]long
BINARYbyte[][]byte[]
BITboolean[]boolean
DATEjava.sql.Date[] java.sql.Date
DOUBLEdouble[]double
FLOATdouble[]double
INTEGERint[]int
LONGVARBINARYbyte[][]byte[]
REALfloat[]float
SMALLINTshort[]short
TIMEjava.sql.Time[] java.sql.Time
TIMESTAMPjava.sql.Timestamp[] java.sql.Timestamp
VARBINARYbyte[][]byte[]
OTHERyourType[] yourType
JAVA_OBJECT (only valid in Java2/JDBC 2.0 environments)yourType[] yourType

Register the output type of the parameter before executing the call. For INOUT parameters, it is good practice to register the output parameter before setting its input value.


Previous Page
Next Page
Table of Contents
Index