You are on page 1of 4

Create PL/SQL Package Procedure and function:

Note: for sake of understanding i am keeping things simple


--Table to wok with Procedure.
createtablelogger
(log_timetimestamp)
tablespaceusers;
-- Procedure
createorreplaceprocedureinsert_into_loggeras
pragmaautonomous_transaction;
begin
insertintologger
values(systimestamp);
commit;
endinsert_into_logger;

-- Function

createorreplacefunctionget_emp_fullname(empidinnumber)returnvarchar2
as
v_full_namevarchar2(120);
begin
selectfirst_name||''||last_name
intov_full_name
fromemployees
whereemployee_id=empid;

returnv_full_name;
exception
whenno_data_foundthen
returnnull;
endget_emp_fullname;

Go to Model and Generate the java class of the Application Module.

Now open the Application Module.java file and paste the below code.

private static int VARCHAR2 = Types.VARCHAR;


protected Object callStoredFunction(int sqlReturnType, String stmt,
Object[] bindVars) {
CallableStatement st = null;
try {
// 1. Create a JDBC CallabledStatement
st =
getDBTransaction().createCallableStatement("begin ? := " + stmt + ";end;",
0);
// 2. Register the first bind variable for the return value
st.registerOutParameter(1, sqlReturnType);
if (bindVars != null) {
// 3. Loop over values for the bind variables passed in, if any
for (int z = 0; z < bindVars.length; z++) {
// 4. Set the value of user-supplied bind vars in the stmt
st.setObject(z + 2, bindVars[z]);
}
}
// 5. Set the value of user-supplied bind vars in the stmt
st.executeUpdate();
// 6. Return the value of the first bind variable

return st.getObject(1);
} catch (SQLException e) {
throw new JboException(e);
} finally {
if (st != null) {
try {
// 7. Close the statement
st.close();
} catch (SQLException e) {
Number a ;
}
}
}
}

Put below methods also in the java file

public String getEmpFullName(Number n) {


return (String)callStoredFunction(VARCHAR2, "get_emp_fullname(?)",
new Object[] { n });
}

public void logProcedure() {


getDBTransaction().executeCommand("begin insert_into_logger; end;");
}

Go to again Application Module and below Java class and click Client Interface
to Expose methods on Client interface. Shuttle the methods to selected area
and Click OK

Now create a JSF page and drag and drop the Logprocedure as a ADF Button
and test it.

Drag the getEmpFullname function to the page and choose ADF parameter form (it is a function
so we have to bind the input and return values)

Drag and drop the String (output parameter) as Output Text

Output

The page on design time look like this

You might also like