Calling PL SQL Function
Calling PL SQL Function
html
http://sameh-nassar.blogspot.in/2010/01/create-plsql-function-and-call-it-from.html
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:
(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
pragma autonomous_transaction;
begin
values(systimestamp);
commit;
end insert_into_logger;
-- Function
?
1
2
3
4
5
into v_full_name
from employees
9
10
11
12
13
14
return v_full_name;
exception
when no_data_found then
return null;
end get_emp_fullname;
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
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
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
4
5
CallableStatement st = null;
try {
7
8
9
10
0);
11
12
st.registerOutParameter(1, sqlReturnType);
13
if (bindVars != null) {
// 3. Loop over values for the bind variables passed in, if any
14
15
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
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
?
3
4
2
3
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 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
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
- 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
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.
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.
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
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.
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 :
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
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
generate AppModuleImplfile.
and write this function
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.
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