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
only works for , Version 9.4 or
above. For more information about UDRs, see HCL OneDB™ User-Defined
Routines and Data Types Developer's Guide and HCL® J/Foundation Developer's
Guide.
import java.sql.*;
public class myudr {
public myudr() {
}
public static void main(String args[]) {
Connection myConn = null;
try {
myConn = DriverManager.getConnection(
"jdbc:onedb://MYSYSTEM:18551/testDB;"
+"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();
}
}
}
- - -
$> java ... myudr
return value from myudr = true
arg1 OUT parameter value = 4
arg3 OUT parameter value = 8