4/13/23, 10:31 AM Java CallableStatement - javatpoint
Home Java Programs OOPs String Exception Multithreading
[Link] 1/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
Java CallableStatement Interface
CallableStatement interface is used to call the stored procedures and functions.
We can have business logic on the database by the use of stored procedures and functions that will
make the performance better because these are precompiled.
Suppose you need the get the age of the employee based on the date of birth, you may create a
function that receives date as the input and returns age of the employee as the output.
What is the difference between stored procedures and functions.
The differences between stored procedures and functions are given below:
Stored Procedure Function
is used to perform business logic. is used to perform calculation.
must not have the return type. must have the return type.
may return 0 or more values. may return only one values.
We can call functions from the procedure. Procedure cannot be called from function.
Procedure supports input and output Function supports only input parameter.
parameters.
Exception handling using try/catch block can be Exception handling using try/catch can't be
used in stored procedures. used in user defined functions.
[Link] 2/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
A Solo Sea Adventure
Awaits
IndiGo
How to get the instance of CallableStatement?
The prepareCall() method of Connection interface returns the instance of CallableStatement. Syntax
is given below:
public CallableStatement prepareCall("{ call procedurename(?,?...?)}");
The example to get the instance of CallableStatement is given below:
CallableStatement stmt=[Link]("{call myprocedure(?,?)}");
It calls the procedure myprocedure that receives 2 arguments.
Full example to call the stored procedure using JDBC
To call the stored procedure, you need to create it in the database. Here, we are assuming that
stored procedure looks like this.
create or replace procedure "INSERTR"
(id IN NUMBER,
name IN VARCHAR2)
is
begin
insert into user420 values(id,name);
end;
[Link] 3/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
The table structure is given below:
create table user420(id number(10), name varchar2(200));
In this example, we are going to call the stored procedure INSERTR that receives id and name as the
parameter and inserts it into the table user420. Note that you need to create the user420 table as
well to run this application.
import [Link].*;
public class Proc {
public static void main(String[] args) throws Exception{
[Link]("[Link]");
Connection con=[Link](
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
CallableStatement stmt=[Link]("{call insertR(?,?)}");
[Link](1,1011);
[Link](2,"Amit");
[Link]();
[Link]("success");
}
}
Now check the table in the database, value is inserted in the user420 table.
Example to call the function using JDBC
In this example, we are calling the sum4 function that receives two input and returns the sum of the
given number. Here, we have used the registerOutParameter method of CallableStatement
interface, that registers the output parameter with its corresponding type. It provides information to
the CallableStatement about the type of result being displayed.
[Link] 4/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
The Types class defines many constants such as INTEGER, VARCHAR, FLOAT, DOUBLE, BLOB, CLOB
etc.
Let's create the simple function in the database first.
create or replace function sum4
(n1 in number,n2 in number)
return number
is
temp number(8);
begin
temp :=n1+n2;
return temp;
end;
/
Now, let's write the simple program to call the function.
import [Link].*;
public class FuncSum {
public static void main(String[] args) throws Exception{
[Link]("[Link]");
Connection con=[Link](
"jdbc:oracle:thin:@localhost:1521:xe","system","oracle");
CallableStatement stmt=[Link]("{?= call sum4(?,?)}");
[Link] 5/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
[Link](2,10);
[Link](3,43);
[Link](1,[Link]);
[Link]();
[Link]([Link](1));
}
}
Output: 53
<<Prev Next>>
For Videos Join Our Youtube Channel: Join Now
Feedback
Send your Feedback to feedback@[Link]
Help Others, Please Share
[Link] 6/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
Learn Latest Tutorials
Splunk tutorial SPSS tutorial Swagger T-SQL tutorial
tutorial
Splunk SPSS Transact-SQL
Swagger
Tumblr tutorial React tutorial Regex tutorial Reinforcement
learning tutorial
Tumblr ReactJS Regex
Reinforcement
Learning
R Programming RxJS tutorial React Native Python Design
tutorial tutorial Patterns
RxJS
R Programming React Native Python Design
Patterns
Python Pillow Python Turtle Keras tutorial
tutorial tutorial
Keras
Python Pillow Python Turtle
Preparation
Aptitude Logical Verbal Ability Interview
Reasoning Questions
Aptitude Verbal Ability
Reasoning Interview Questions
Company
Interview
Questions
Company Questions
Trending Technologies
[Link] 7/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
Artificial AWS Tutorial Selenium Cloud
Intelligence tutorial Computing
AWS
Artificial Selenium Cloud Computing
Intelligence
Hadoop tutorial ReactJS Data Science Angular 7
Tutorial Tutorial Tutorial
Hadoop
ReactJS Data Science Angular 7
Blockchain Git Tutorial Machine DevOps
Tutorial Learning Tutorial Tutorial
Git
Blockchain Machine Learning DevOps
[Link] / MCA
DBMS tutorial Data Structures DAA tutorial Operating
tutorial System
DBMS DAA
Data Structures Operating System
Computer Compiler Computer Discrete
Network tutorial Design tutorial Organization and Mathematics
Architecture Tutorial
Computer Network Compiler Design
Computer Discrete
Organization Mathematics
Ethical Hacking Computer Software html tutorial
Graphics Tutorial Engineering
Ethical Hacking Web Technology
[Link] 8/9
4/13/23, 10:31 AM Java CallableStatement - javatpoint
Computer Graphics Software
Engineering
Cyber Security Automata C Language C++ tutorial
tutorial Tutorial tutorial
C++
Cyber Security Automata C Programming
Java tutorial .Net Python tutorial List of
Framework Programs
Java Python
tutorial
Programs
.Net
Control Data Mining Data
Systems tutorial Tutorial Warehouse
Tutorial
Control System Data Mining
Data Warehouse
[Link] 9/9