Unit-4 PPT-5
Unit-4 PPT-5
CallableStatement
• In our programming if any code repeatedly required, then we
can define that code inside a method and we can call that
method multiple times based on our requirement.
• Hence method is the best reusuable component in our
programming.
• Similarly in the database programming, if any group of sql
statements is repeatedly required then we can define those sql
statements in a single group and we can call that group
repeatedly based on our requirement.
• This group of sql statements that perform a particular task is
nothing but Stored Procedure.Hence stored procedure is the
best reusable component at database level.
• Hence Stored Procedure is a group of sql statements that
performs a particular task.
• These procedures stored in database permanently for future
purpose and hence the name stored procedure.
• Usually stored procedures are created by Database Admin
(DBA).
• Every database has its own language to create Stored
Procedures.
Oracle has ➔ PL/SQL
MySQL has ➔ Stored Procedure Language
Microsoft SQL Server has ➔ Transact SQL(TSQL )
• Similar to methods stored procedure has its own parameters.
• Stored Procedure has 3 Types of parameters.
1. IN parameters(to provide input values)
2. OUT parameters(to collect output values)
3. INOUT parameters(to provide input and to collect output)
Eg 1 :
Z:=X+Y;
X,Y are IN parameters and Z is OUT parameter
Eg 2:
X:=X+X;
X is INOUT parameter
Syntax for creating Stored Procedure (Oracle):
1) create or replace procedure procedure1(X IN number, Y IN number,Z OUT
number) as
2) BEGIN
3) z:=x+y;
4) END;
Note:
SQL and PL/SQL are not case-sensitive languages. We can use lower
case and upper case also.
After writing Stored Procedure, we have to compile for this we
required to use "/" (forward slash)
/ ➔ For compilation
while compiling if any errors occurs,then we can check these errors
by using the following command
SQL> show errors;
Once we created Stored Procedure and compiled successfully, we
have to register OUT parameter to hold result of stored procedure.
SQL> variable sum number; (declaring a variable)
We can execute with execute command as follows
SQL> execute procedure1(10,20,:sum);
SQL> print sum;
Eg 2:
1) create or replace procedure procedure1(X IN number,Y OUT number) as
2) BEGIN
3) Y:= x*x;
4) END;
5) /
SQL> variable square number;
SQL> execute procedure1(10,:square);
SQL> print square;
SQUARE
----------
100
Eg3: Procedure To Print Employee Salary Based On Given Employee
Number.
1) create or replace procedure procedure2(eno1 IN number,esal1 OUT
number) as
2) BEGIN
3) select esal into esal1 from employees where eno=eno1;
4) END;
5) /
6. Get the result from OUT parameter by using the corresponding getXxx()
method.
Eg: int result=cst.getInt(3);
Stored Procedures App1: JDBC Program to call StoredProcedure
which can take two input numbers and produces the result.
Stored Procedure:
1) create or replace procedure addProc(num1 IN number,num2 IN
number,num3 OUT number) as
2) BEGIN
3) num3 :=num1+num2;
4) END;
5) /
• StoredProceduresDemo1.java
import java.sql.*;
2) class StoredProceduresDemo1
3) {
4) public static void main(String[] args) throws Exception
5) {
6) Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system",“CS");
7) CallableStatement cst=con.prepareCall("{call addProc(?,?,?)}");
8) cst.setInt(1,100);
9) cst.setInt(2,200);
10) cst.registerOutParameter(3,Types.INTEGER);
11) cst.execute();
12) System.out.println("Result.."+cst.getInt(3));
13) con.close(); } }
Stored Procedures App2: JDBC Program to call StoredProcedure
which can take employee number as input and provides
corresponding salary.
Stored Procedure:
1) create or replace procedure getSal(id IN number,sal OUT number) as
2) BEGIN
3) select esal into sal from employees where eno=id;
4) END;
5) /
• StoredProceduresDemo2.java
1) import java.sql.*;
2) class StoredProceduresDemo2
3) {
4) public static void main(String[] args) throws Exception
5) {
6) Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system",“cs");
7) CallableStatement cst=con.prepareCall("{call getSal(?,?)}");
8) cst.setInt(1,100);
9) cst.registerOutParameter(2,Types.FLOAT);
10) cst.execute();
11) System.out.println("Salary ..."+cst.getFloat(2));
12) con.close(); } }
Stored Procedures App3: JDBC Program to call StoredProcedure
which can take employee number as input and provides
corresponding name and salary.
Stored Procedure:
1) create or replace procedure getEmpInfo(id IN number,name OUT
varchar2,sal OUT number) as
2) BEGIN
3) select ename,esal into name,sal from employees where eno=id;
4) END;
5) /
StoredProceduresDemo3.java
1) import java.sql.*;
2) class StoredProceduresDemo3 {
4) public static void main(String[] args) throws Exception {
6) Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system","durga");
7) CallableStatement cst=con.prepareCall("{call getEmpInfo(?,?,?)}");
8) cst.setInt(1,100);
9) cst.registerOutParameter(2,Types.VARCHAR);
10) cst.registerOutParameter(3,Types.FLOAT);
11) cst.execute();
12) System.out.println("Employee Name is :"+cst.getString(2));
13) System.out.println("Employee Salary is :"+cst.getFloat(3));
14) con.close();
15) } }
CURSORS
• The results of SQL Queries will be stored in special memory area inside
database software.
• This memory area is called Context Area. To access Results of this
context area, Some pointers are required and these pointers are
nothing but cursors.
• Hence the main objective of cursor is to access results of SQL Queries.
• There are 2 types of cursors
1. Implicit cursors
2. Explicit cursors
1. Implicit cursors:
These cursors will be created automatically by database software to
hold results whenever a particular type of sql query got executed.
2. Explicit Cursors:
These cursors will be created explicitly by the developer to hold results
of particular sql queries.
Eg 1: SYS_REFCURSOR can be used to access result of select query i.e to access
ResultSet.
Eg 2: %ROWCOUNT is an implicit cursor provided by Oracle to represent the
number of rows effected b'z of insert,delete and update queries.
Eg 3: %FOUND is an implicit cursor provided by Oracle to represent whether any
rows effected or not b'z of insert,delete and update operations(non-select query)
SYS_REFCURSOR VS OracleTypes.CURSOR:
• To register SYS_REFCURSOR type OUT parameter JDBC does not
contain any type.
• To handle this situation, Oracle people provided
OracleTypes is a java class present in oracle.jdbc package and it is
available as the part of ojdbc6.jar
If OUT parameter is SYS_REFCURSOR type,then we can get ResultSet
by using getObject() method. But return type of getObject() method
is Object and hence we should perform typecasting.
ResultSet rs = (ResultSet)cst.getObject(1);
Eg:
1) create or replace prodecure getAllEmpInfo(emps OUT SYS_REFCURSOR) as
2) BEGIN
3) OPEN emps for
4) select * from employees;
5) end;
6) /
1) CallableStatement cst=con.prepareCall("{ call getAllEmpInfo(?)}");
2) cst.registerOutParameter(1,OracleTypes.CURSOR);
3) cst.execute();
4) RS rs = (RS)cst.getObject(1);
5) while(rs.next())
6) {
7) SOP(rs.getInt(1)+".."+rs...);
8) }
Stored Procedures App4: JDBC Program to call StoredProcedure
which returns all Employees info by using SYS_REFCURSOR
Stored Procedure:
1) create or replace procedure getAllEmpInfo1(sal IN number,emps OUT SYS_REFCURSOR) as
2) BEGIN
3) open emps for
4) select * from employees where esal<sal;
5) END;
6) /
StoredProceduresDemo4.java
1) import java.sql.*;
2) import oracle.jdbc.*;// for OracleTyes.CURSOR and it is present in ojdbc6.jar
3) class StoredProceduresDemo4 {
5) public static void main(String[] args) throws Exception {
7) Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","system",“CS");
8) CallableStatement cst=con.prepareCall("{call getAllEmpInfo1(?,?)}");
9) cst.setFloat(1,6000);
10) cst.registerOutParameter(2,OracleTypes.CURSOR);
11) cst.execute();
12) ResultSet rs = (ResultSet)cst.getObject(2);
13) boolean flag=false;
14) System.out.println("ENO\tENAME\tESAL\tEADDR");
15) System.out.println("-----------------------");
16) while(rs.next())
17) {
18) flag=true;
19) System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\
t"+rs.getString(4));
20) }
21) if(flag== false)
22) {
23) System.out.println("No Recors Available");
24) }
25) con.close();
26) }
27) }
Stored Procedures App5: JDBC Program to call StoredProcedure which returns
all Employees info by using SYS_REFCURSOR based initial characters of the
name
Stored Procedure:
1) create or replace procedure getAllEmpInfo2(initchars IN varchar,emps OUT
SYS_REFCURSOR) as
2) BEGIN
3) open emps for
4) select * from employees where ename like initchars;
5) END;
6) /
StoredProceduresDemo5.java
1) import java.sql.*;
2) import java.util.*;
3) import oracle.jdbc.*;// for OracleTyes.CURSOR and it is present in
ojdbc6.jar
4) class StoredProceduresDemo5 {
6) public static void main(String[] args) throws Exception {
8) Connection con =
DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE","syst
em",“CS");
9) CallableStatement cst=con.prepareCall("{call getAllEmpInfo2(?,?)}");
10) Scanner sc = new Scanner(System.in);
11) System.out.println("Enter initial characters of the name");
12) String initialchars=sc.next()+"%";
13) cst.setString(1,initialchars);
14) cst.registerOutParameter(2,OracleTypes.CURSOR);
15) cst.execute();
16) ResultSet rs = (ResultSet)cst.getObject(2);
17) boolean flag= false;
18) System.out.println("ENO\tENAME\tESAL\tEADDR");
19) System.out.println("-----------------------");
20) while(rs.next())
21) {
22) flag=true;
23) System.out.println(rs.getInt(1)+"\t"+rs.getString(2)+"\t"+rs.getFloat(3)+"\
t"+rs.getString(4));
24) }
25) if(flag== false)
26) {
27) System.out.println("No Recors Available");
28) }
29) con.close();
30) }
31) }