Java calls Oracle functions. A function is essentially a stored procedure that returns a result. This example shows how to call functions with in, out, and in/out parameters.
Similar methods.
CallableStatement cs;
Try {
// Call a function without parameters. The function returns a VARCHAR.
// Pre-processes callable statements
Cs = connection. prepareCall ("{? = Call myfunc }");
// Register the return value type
Cs. registerOutParameter (1, I );
// Execute and retrieve the returned value
Cs.exe cute ();
String retValue = cs. getString (1 );
// Call a function with an in parameter; the function returns a VARCHAR
Cs = connection. prepareCall ("{? = Call myfuncin (?)} ");
// Register the type of the return value
Cs. registerOutParameter (1, Types. VARCHAR );
// Set the value for the IN parameter
Cs. setString (2, "a string ");
// Execute and retrieve the returned value
Cs.exe cute ();
RetValue = cs. getString (1 );
// Call a function with an out parameter; the function returns a VARCHAR
Cs = connection. prepareCall ("{? = Call myfuncout (?)} ");
// Register the types of the return value and OUT parameter
Cs. registerOutParameter (1, Types. VARCHAR );
Cs. registerOutParameter (2, Types. VARCHAR );
// Execute and retrieve the returned values
Cs.exe cute ();
RetValue = cs. getString (1); // return value
String outParam = cs. getString (2); // OUT parameter
// Call a function with an in/out parameter; the function returns a VARCHAR
Cs = connection. prepareCall ("{? = Call myfuncinout (?)} ");
// Register the types of the return value and OUT parameter
Cs. registerOutParameter (1, Types. VARCHAR );
Cs. registerOutParameter (2, Types. VARCHAR );
// Set the value for the IN/OUT parameter
Cs. setString (2, "a string ");
// Execute and retrieve the returned values
Cs.exe cute ();
RetValue = cs. getString (1); // return value
OutParam = cs. getString (2); // IN/OUT parameter
} Catch (SQLException e ){
}
Replace () for Oracle Functions ()
Oracle Functions
Differences in case and decode usage and Performance Comparison of Oracle Functions
Simple Oracle functions and stored procedures
The Oracle function obtains the time period in seconds or minutes.