Está en la página 1de 20

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.

html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

Calling PL/SQL Procedure and Function in Oracle ADF


application
Email ThisBlogThis!Share to TwitterShare to FacebookShare to Pinterest
Hi all,
Migrating Oracle forms applications to ADF the one of the biggest issue is what to do with you existing PL/SQL code.
As we all aware that ADF is JEE technology based and it cannot directly interact with PL/SQL as we used to do in
Oracle forms application.
The simplest solution is to migrate your existing code to the database and use that PL/SQL as exposed web-service
in our ADF application and today i will show you the example of it.
Where should the business logic code resides?
Its a million dollar question and my views that It depends on application to application. If we analyze that this code
can work inside database so we should move it to database and if the logic depends on some UI interaction (e.g
looping over the rows etc etc) then it should be the combination of both.
Mr.Tom Kyte (asktom.oracle.com) shared his experience in one of his presentations about migrating business logic
code to database and i agreed with him in some context.
Migrating business logic code to database is better choice as we can manage our code centralized and also you
never know what will happen in next 5 to 10 years technology changing quickly today Java, ADF is HOT tomorrow
who knows :) so logic stored in database will save us from major re-writing the same code again.

In this example:
- We will use Oracle default HR Schema.
- We will create a stored package in PL/SQL which will insert some data into table.
- We will create a stored function which will return the complete name of Employee i.e First_name||' '||Last_name.
- We will add a code in our Application ModuleImpl Class and expose all methods to client interface.
- Integrate those methods on JSF page.
I assumed that you are already setup a project with at least Application Module (AM)
Create PL/SQL Package Procedure and function:

Note: for sake of understanding i am keeping things simple


--Table to wok with Procedure.
?

create table logger

(log_time timestamp)

tablespace users;

-- Procedure

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

create or replace procedure insert_into_logger as

pragma autonomous_transaction;

begin

insert into logger

values(systimestamp);

commit;

end insert_into_logger;

-- Function
?

1
2
3
4
5

create or replace function get_emp_fullname (empid in number) return varchar2


as
v_full_name varchar2(120);
begin
select first_name||' '||last_name

into v_full_name

from employees

where employee_id = empid;

9
10
11
12
13
14

return v_full_name;
exception
when no_data_found then
return null;
end get_emp_fullname;

Generate Java Class for Application Module


Double click the Application Module and click the Pencil Icon appearing with the Java classes.

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

- Check the first checkbox and Press OK

Double click the AppModuleImpl.java under your Application Module and add the following code note that this code is
based on my example if you function and procedure taking more argument you need to slightly change the code you
can check examples here

1
2

private static int VARCHAR2 = Types.VARCHAR;

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html
3

protected Object callStoredFunction(int sqlReturnType, String stmt,


Object[] bindVars) {

4
5

CallableStatement st = null;

try {

7
8
9

// 1. Create a JDBC CallabledStatement


st =
getDBTransaction().createCallableStatement("begin ? := " + stmt +
";end;",

10

0);

11

// 2. Register the first bind variable for the return value

12

st.registerOutParameter(1, sqlReturnType);

13

if (bindVars != null) {
// 3. Loop over values for the bind variables passed in, if any

14

for (int z = 0; z < bindVars.length; z++) {

15

// 4. Set the value of user-supplied bind vars in the stmt

16

st.setObject(z + 2, bindVars[z]);

17
}

18
19
20
21

}
// 5. Set the value of user-supplied bind vars in the stmt
st.executeUpdate();
// 6. Return the value of the first bind variable

22

return st.getObject(1);

23

} catch (SQLException e) {

24
25
26
27

throw new JboException(e);


} finally {
if (st != null) {
try {

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html
28
// 7. Close the statement

29

st.close();

30

} catch (SQLException e) {

31

32

33
34

}
}

35
- Add Method to call database functions
?

public String getEmpFullName(Number n) {

return (String)callStoredFunction(VARCHAR2, "get_emp_fullname(?)",


new Object[] { n });

3
4

public void logProcedure() {

2
3

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


}

- Right click and choose Make to compile the file.


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

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

- Open the JSF page and expand the data control


- Drag the logProcedure Method to the page and choose ADF button

- 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)

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

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

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

- The page on design time look like this

- Right click and choose Run . Enter 100 in the Input Parameter and Press Get Employee Name Button.
- Press Test Log procedure button couple of times.

- Query the logger table as we can see the procedure inserting timestamp data

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

Call PL/SQL function in Backing Bean Using


Application Module Client Interface
Posted by: Manish Pandey March 13, 2014 in ADF Leave a comment 6251 Views

Today Im going to discuss about a very common use case of how to call pl/sql function in backing Bean as the
database connection object is not available in backing bean.

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

Heres step by step procedure to call pl/sql function in backing been using Client Interface.

Sample Code
1. Create ApplicationModuleImpl class.

2. Write following line of code to call database function (getAmtText is a pl/sql function which return enter no to Words
Format.)
view plaincopy to clipboardprint?

1.
2.
3.
4.

public String getAmtText(String Amt)


{
String amttext="";
String sp = "{? = call (amt_text(?))}";

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
16.
17.
18.
19.

Connection conn = null;


CallableStatement proc = null;
try {
proc = getDBTransaction().createCallableStatement(sp,0);
proc.registerOutParameter(1, OracleTypes.VARCHAR);
proc.setObject(2, Amt);
proc.executeQuery();
amttext= proc.getString(1);
}
catch(Exception e)
{
System.out.println(e);
}
return amttext;
}

3. Add function to Client Interface.

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

4. Bind component on jspx page as MethodAction

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

5. Write follwing lines of code to excute ClientInterface Method action on Backing Bean.
view plaincopy to clipboardprint?

1.
2.
3.
4.
5.
6.
7.
8.

public void ConvertToText(ActionEvent actionEvent) {


BindingContext bindingctx=BindingContext.getCurrent();
BindingContainer binding=bindingctx.getCurrentBindingsEntry();
OperationBinding operationBinding1 = binding.getOperationBinding("getAmtText");

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html
9.
10.
11.
12.
13.
14.
15.
16.
17.

operationBinding1.getParamsMap().put("Amt",it1.getValue());
operationBinding1.execute();
String amttext =(String)operationBinding1.getResult();
ot1.setValue(amttext);
}

Result :

Create PL/SQL Function And Call It From Your ADF Application


In many situations you need to create PL/SQL function in your database and call it from your
application. This post will illustrate how to create PL/SQL function in your database and how to call this
function. The next example will be applied in HR Schema which we will create a PL function that take
an employee Id and return his name. Follow these steps:
1- Create new ADF Application assume Application name "UsingPLSQLFunction_Application".
2- Create new page assume its name "index.jspx".
3- Your Application should like this

4- Right-click in your model project -- > new -->


ADF Business Components --> Business Components From Tables. Then Press Ok.

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

5- Choose you database connection (assume HR


Schema).
6-Choose EMPLOYEES Table as an Entity. Then Choose Employees as Updatable view object. Then
press in finish button.
7- Open your database navigator then open your application connection then right-click on Function
then choose New Function.

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

8- Enter Function Name as

"GET_EMPLOYEE_NAME" then press ok.


this function :
CREATE OR REPLACE FUNCTION GET_EMPLOYEE_NAME (emp_id NUMBER)
RETURN VARCHAR2
AS
emp_name VARCHAR2(20);
BEGIN
select FIRST_NAME into emp_name from employees
where EMPLOYEE_ID=emp_id;
RETURN emp_name;
END GET_EMPLOYEE_NAME;

9- Write

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

10 - Open your Application Module and

generate AppModuleImplfile.
and write this function

11- Open this file

imports:
import java.sql.CallableStatement;
import java.sql.SQLException;import java.sql.Types;
import oracle.jbo.JboException;
public String getEmployeeName(int empId){
CallableStatement cs=null;
try{
cs=getDBTransaction().createCallableStatement("begin ? := GET_EMPLOYEE_NAME(?); end;",0);
cs.registerOutParameter(1, Types.VARCHAR);
cs.setInt(2, empId);

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

cs.executeUpdate();
return cs.getString(1);
}catch(SQLException e){
throw new JboException(e);
}
}
12- Open AppModule -- > java --> client Interface then shuttle this function.

13- From your Data Control drag your function in

the page then choose ADF Parameter Form.


14- Right-click in the generated button then choose "Create Method Binding For Action" then create a
manged bean.

http://www.baigzeeshan.com/2010/05/calling-plsql-procedure-and-function-in.html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html

15- The commandButton Method will be like this


public String cb1_action() {
BindingContainer bindings = getBindings();
OperationBinding operationBinding = bindings.getOperationBinding("getEmployeeName");
Object result = operationBinding.execute();
System.out.println("Result= " + result); // result will be the output of PL function
if (!operationBinding.getErrors().isEmpty()) {
return null;
}
return null;
}
16- Save your Application then run index.jspx.