×

Callable Statement in Java

The Callable statement in Java is used to call the functions and Stored procedures.

Example:

If we want to know about the age of a person based on their date of birth, we can create a function that can get the age by giving date of birth as input.

Stored Procedure:

The Stored Procedure is used for the logic purpose. It can give both input and output. We can be able to call functions from procedure. The Exception handing can be done in the Stored Procedure. It may return 0 or many values.

Function:

It is used to perform the Calculation. It only works with input parameters. We cannot be able to call functions. The Exception handling cannot be done in Functions. It may return only one value.

Creating a Callable Statement

The prepareCall() method is used to create an object for the Callable statement , this method is present in the Connection Interface. This method takes the String as query input and call Stored procedure and return Callable Statement. The CallableStatements can have both input and output parameters . To give the inputs, we ca use the methods which are there in the CallableStatement Interface.

Example:

CallableStatement Cs = con.prepareCall(“{call myProcedure(?,?)}”);

Input parameters:

We can be able to give the input to the Callable Statement by using the “set” methods. There are two arguments while giving the inputs to the Callable Statements i.e., First argument represent the index as Integer and the Second argument represents the content in String or integer or float etc.

Syntax:

ct.setString(1, “Kotte”);
ct.setString(2, 10000); 

Execution of Callable Statement:

The Execution of the Callable statement is done by the execute() method.

Syntax:

ct.execute();

Example:

For Calling a procedure in Callable Statement, we need to create the procedure in the database and create a table like STUDENT. The procedure is written below:

Create procedure “PRO” (IN Id INT, IN Name VARCHAR(10))

BEGIN

Insert into STUDENT values (Id,Name);

END;

//Example program for Creating new records into Student table using Callable Statement
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class Cs
{
public static void main(String args[])
{
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
String url =“jdbc:mysql://localhost//DB”;
Connection con = DriverManager.getConnection(url,”root”,”password”);
CallableStatement ct = con.prepareCall(“{call PRO}”);
ct.setInt(1, 101);
ct.setString(2, “Kotte”);
ct.setInt(1, 102);
ct.setString(2, “Sai”);


ct.setInt(1, 103);
ct.setString(2, “Karan”);


ct.setInt(1, 104);
ct.setString(2, “Bablu”);


ct.setInt(1, 105);
ct.setString(2, “Krishna”);

//execution of Callable statement.
ct.execute();
System.out.println(“Rows are Inserted”);


} 
}

Output:

Rows Inserted

To check whether the rows are inserted in the database, then we have to use the query like “SELECT * FROM STUDENT;” in the SQL.

mysql>SELECT * FROM STUDENT;
IDName
101Kotte
102Sai
103Karan
104Bablu
105Krishna

The above table is the result of the query “SELECT * FROM STUDENT;”. It has the elements which are inserted using the Callable Statement.

Summary:

The Callable Statements are used to call the functions and Stored procedures.To insert the records in the table in database , we have to create a procedure in the database according to the table then we can able to insert the records into the table using the Callable Statement.


Related Topics

Types of JDBC Drivers

JDBC Drivers: A piece of software known as JDBC Driver permits database communication between Java applications and the server. In order to communicate with our database server, JDBC drivers put into practice...

4 minutes read.

Getting Synchronized Set from Java HashSet

The synchronizedSet() technique for java.util.Collections class is utilized to return a synchronized (string safe) set supported by the predetermined set. To ensure sequential access, it is important that everything admittance...

4 minutes read.

Java Extends keyword

Extends Extends is a keyword which is completely depended on the concept of the inheritance of the java programming language.To understand about of the keyword, we need to learn the concept...

3 minutes read.

Bounded buffer problem in Java

The Bounded buffer Problem can also be called a Producer consumer problem. The problem covers two processes—the producer and the consumer—that share a single, fixed-size buffer that serves as a...

4 minutes read.

Convert IP to Binary in Java

Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking...

4 minutes read.

Char and String differences in Java

Characters in Java Character (char) belongs to the characters group, which represents symbols in a character set, such as alphabets and numerals. A Java char has 16 bits in length and has a range...

5 minutes read.

Java Boolean toString() Method

The toString() method of Java Boolean class returns a String corresponding to this Boolean object. It returns a string value “true”, if the defined object is true else it returns...

2 minutes read.

Java Boolean Keyword

Boolean keyword In java programming language we basically have two types of primitive data types, Boolean and Numeric (integer and floating-point data types). In this article we are going to learn...

4 minutes read.

Group by in Java 8

By using this groupingBy() method, developers can directly able to perform the "GROUP BY" operation. The thing is, now, the Java 8 programming language allows the programmers to do this...

3 minutes read.

Crown Pattern in Java

We know the importance of solving pattern problems. We can solve pattern problems by using any programming language. There is no rule to translating into a particular programming language. We...

4 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

Longest Arithmetic Progression Sequence in Java

The task is to find the length of the largest sequence in an array to form an arithmetic progression. The array arr[] is given. Longest Arithmetic Progression Sequence in Java Algorithm set...

5 minutes read.

Java.net.SocketException

Exception The problem occurred during the execution of the program. If an exception occurs in the program, the program gets terminated. To skip the exception occurring statements, we have to handle...

4 minutes read.

Stack vs Heap in Java

In Java, whenever we declare an object or create a variable, whether it is an instance variable, local variable, or static variable, a certain memory is used to store the...

4 minutes read.

Java String

Definition A string is a series of characters. Consider an example, "Welcome" is a 7-character string. In Java, String is an unchangeable object. That is, the String is perpetual and cannot be replaced after it is created. This is the tutorial in which you...

4 minutes read.

Level order Traversal of a Binary Tree in Java

In Java, a level order traversal of a tree structure is also referred to as the breadth-first traversal of the binary tree. Regarding the subsequent binary tree: Level order traversal is as...

5 minutes read.

Java Integer floatValue() method

The floatValue() method of Integer class returns a float value for this Integer after a widening primitive conversion. Syntax public float floatValue() Parameters NA Specified by This method is specified by floatValue in class Number Return Value This...

1 minute read.

Program to Implement FLAMES Game in Java

Friends, Lovers, Affectionate, Marriage, Enemies, and Sibling are all represented by the acronym FLAMES, which also serves as the name of a well-known game. Although, it can be entertaining to...

4 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Balanced Prime Number in Java

This section will cover the definition of a balanced prime number as well as how to find one using a Java program. Balance Prime Number A prime number that is equivalent to...

5 minutes read.