itGalary : Java (J2EE & J2SE)
Author: JijoSubject: Creating a Stored Procedure,Function in Oracle
Posted: 25 Jan 08 at 4:26pm
Getting the Stored Procedure Names in a Database
This example retrieves the names of all stored procedures in a database.
try { // Get database metadata DatabaseMetaData dbmd = connection.getMetaData(); // Get all stored procedures in any schema and catalog ResultSet resultSet = dbmd.getProcedures(null, null, "%"); // Get stored procedure names from the result set while (resultSet.next()) { String procName = resultSet.getString(3); } } catch (SQLException e) { }
|
Calling a Stored Procedure in a Database
This example demonstrates how to call stored procedures with IN, OUT, and IN/OUT parameters.
CallableStatement cs; try { // Call a procedure with no parameters cs = connection.prepareCall("{call myproc}"); cs.execute(); // Call a procedure with one IN parameter cs = connection.prepareCall("{call myprocin(?)}"); // Set the value for the IN parameter cs.setString(1, "a string"); // Execute the stored procedure cs.execute(); // Call a procedure with one OUT parameter cs = connection.prepareCall("{call myprocout(?)}"); // Register the type of the OUT parameter cs.registerOutParameter(1, Types.VARCHAR); // Execute the stored procedure and retrieve the OUT value cs.execute(); String outParam = cs.getString(1); // OUT parameter // Call a procedure with one IN/OUT parameter cs = connection.prepareCall("{call myprocinout(?)}"); // Register the type of the IN/OUT parameter cs.registerOutParameter(1, Types.VARCHAR); // Set the value for the IN/OUT parameter cs.setString(1, "a string"); // Execute the stored procedure and retrieve the IN/OUT value cs.execute(); outParam = cs.getString(1); // OUT parameter } catch (SQLException e) { }
|
Calling a Function in a Database
A function is essentially a stored procedure that returns a result. This example demonstrates how to call functions with IN, OUT, and IN/OUT parameters.
CallableStatement cs; try { // Call a function with no parameters; the function returns a VARCHAR // Prepare the callable statement cs = connection.prepareCall("{? = call myfunc}"); // Register the type of the return value cs.registerOutParameter(1, i); // Execute and retrieve the returned value cs.execute(); String retValue = cs.getString(1); // Call a function with one 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.execute(); retValue = cs.getString(1); // Call a function with one 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.execute(); retValue = cs.getString(1); // return value String outParam = cs.getString(2); // OUT parameter // Call a function with one 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.execute(); retValue = cs.getString(1); // return value outParam = cs.getString(2); // IN/OUT parameter } catch (SQLException e) { }
|
Creating a Stored Procedure or Function in an Oracle Database
A stored procedure or function can be created with no parameters, IN parameters, OUT parameters, or IN/OUT parameters. There can be many parameters per stored procedure or function.
An IN parameter is a parameter whose value is passed into a stored procedure/function module. The value of an IN parameter is a constant; it can't be changed or reassigned within the module.
An OUT parameter is a parameter whose value is passed out of the stored procedure/function module, back to the calling PL/SQL block. An OUT parameter must be a variable, not a constant. It can be found only on the left-hand side of an assignment in the module. You cannot assign a default value to an OUT parameter outside of the module's body. In other words, an OUT parameter behaves like an uninitialized variable.
An IN/OUT parameter is a parameter that functions as an IN or an OUT parameter or both. The value of the IN/OUT parameter is passed into the stored procedure/function and a new value can be assigned to the parameter and passed out of the module. An IN/OUT parameter must be a variable, not a constant. However, it can be found on both sides of an assignment. In other words, an IN/OUT parameter behaves like an initialized variable.
This example creates stored procedures and functions demonstrating each type of parameter.
try { // To create a connection to an Oracle database, // see e235 Connecting to an Oracle Database Statement stmt = connection.createStatement(); // Create procedure myproc with no parameters String procedure = "CREATE OR REPLACE PROCEDURE myproc IS " + "BEGIN " + "INSERT INTO oracle_table VALUES('string 1'); " + "END;"; stmt.executeUpdate(procedure); // Create procedure myprocin with an IN parameter named x. // IN is the default mode for parameter, so both `x VARCHAR' and `x IN VARCHAR' are valid procedure = "CREATE OR REPLACE PROCEDURE myprocin(x VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES(x); " + "END;"; stmt.executeUpdate(procedure); // Create procedure myprocout with an OUT parameter named x procedure = "CREATE OR REPLACE PROCEDURE myprocout(x OUT VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES('string 2'); " + "x := 'outvalue'; " // Assign a value to x + "END;"; stmt.executeUpdate(procedure); // Create procedure myprocinout with an IN/OUT parameter named x; // x functions as an IN parameter and also as an OUT parameter procedure = "CREATE OR REPLACE PROCEDURE myprocinout(x IN OUT VARCHAR) IS " + "BEGIN " + "INSERT INTO oracle_table VALUES(x); " // Use x as IN parameter + "x := 'outvalue'; " // Use x as OUT parameter + "END;"; stmt.executeUpdate(procedure); // Create a function named myfunc which returns a VARCHAR value; // the function has no parameter String function = "CREATE OR REPLACE FUNCTION myfunc RETURN VARCHAR IS " + "BEGIN " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); // Create a function named myfuncin which returns a VARCHAR value; // the function has an IN parameter named x function = "CREATE OR REPLACE FUNCTION myfuncin(x VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "RETURN 'a return string'||x; " + "END;"; stmt.executeUpdate(function); // Create a function named myfuncout which returns a VARCHAR value; // the function has an OUT parameter named x whose value is // returned to the calling PL/SQL block when the execution of the function ends function = "CREATE OR REPLACE FUNCTION myfuncout(x OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "x:= 'outvalue'; " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); // Create a function named myfuncinout that returns a VARCHAR value; // the function has an IN/OUT parameter named x. As an IN parameter, the value of x is // defined in the calling PL/SQL block before it is passed in eyfuncinout // function. As an OUT parameter, the new value of x, `x value||outvalue', is also // returned to the calling PL/SQL block when the execution of the function ends. function = "CREATE OR REPLACE FUNCTION myfuncinout(x IN OUT VARCHAR) RETURN VARCHAR IS " + "BEGIN " + "x:= x||'outvalue'; " + "RETURN 'a returned string'; " + "END;"; stmt.executeUpdate(function); } catch (SQLException e) { }
|