×

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

Java String equalsIgnoreCase() method

equalsIgnoreCase() method compares two Strings based on the their content but ignore the case. Syntax public boolean equalsIgnoreCase(Objects anObject) Parameter anObject: Object to be compared with the current String without case consideration. Returns It returns true...

1 minute read.

Read JSON file in Java

 To know about reading a JSON file in Java first we must know about the JSON file. JSON: JSON is “JavaScript Object Notation”. The JSON is the simple format for storing and...

3 minutes read.

Java DatagramSocket and Java DatagramPacket

Datagrams TCP/IP style networking specifies a serialized, predictable, and reliable stream of data in the form of a packet. Servers and clients communicate through a reliable channel, such as TCP socket, have a dedicated...

6 minutes read.

Java Boolean Class

The Boolean class wraps a value of the Boolean primitive in an object. Its object contains only a single field whose type is Boolean. Boolean Methods: This class contains several different methods...

2 minutes read.

Java IO

It is a part of java libraries but is often known as I/O streams, file I/O, and file handling. The Java I/O concept satisfies the need for input processing and...

4 minutes read.

Java Create File

A File is a hypothetical way, which has no genuine presence. It is very much like while "using" that File that the major real activity of putting away something in...

5 minutes read.

Java 8 Multimap

Java comes with several practical built-in collection libraries. However, there are situations when we need specialized collections that are not included in the Java standard library. The Multimap is one...

7 minutes read.

Java Font

The font is a Java class that is a part of java.awt package. The Serializable interface is implemented by it. The direct recognized child of a Java Font class is...

8 minutes read.

Java md5 Hash Example

A 128-bit hash value is generated by the Message Digest Algorithm 5, which is a cryptographic algorithm. A stationary hash value is generated by the hash function from data of...

3 minutes read.

Java Byte Code

Java byte code is really a powerful mechanism which makes Java a portable and platform-independent programming language. There are two software components which go along and make this byte code...

3 minutes read.

Java LinkedList

LinkedList Java The Java LinkedList is used to store the elements by using a doubly linked list. It contains the duplicate items, also maintains the order of the insertion, the class...

5 minutes read.

Java String toLowerCase() methods

Java String toLowerCase() method is used to convert all the characters of the String into lower case. Syntax: public String toLowerCase()              public String toLowerCase(Locale locale) Returns: It returns Lower...

1 minute read.

String Palindrome Program in Java

String Palindrome Program in Java The palindrome is a string, phrase, word, number, or other sequences of characters that can be read in both directions i.e. forward (left to right) and...

11 minutes read.

Java Math min() Method

The min() method of Math class returns the smaller of two arguments. The arguments can be of double, float, int or long data type. Syntax: public static double min (double a, double...

2 minutes read.

Minimum Difference Between Groups of Size Two in Java

There is given an array with various integers in it. The goal is to divide the elements into distinct groups, each of which has just two, so that the difference...

3 minutes read.

How annotations work in Java

Annotations were introduced by sun microsystem and are available from the java 1.5 version. In general, configurations can be done either by XML or by annotations. Hibernate and spring framework users prefer...

4 minutes read.

Java Integer min() method

The min()  method of Integer class returns the smaller of two int values. It returns the same result as by calling Math.min.  Syntax public static int min(int a, int b) Parameters The parameters ‘a’...

2 minutes read.

Java Binary Operators

In this article, we are going to discuss about the binary operators in Java and their operations. Also, an example program will be discussed along with the operations. These are...

6 minutes read.

String Matches in Java

Firstly we have to know something about String? Strings are a bundle of different characters that are normally used in Java programming language. Strings are regarded as objects in the Java...

3 minutes read.

Java Math addExact() Method

The addExact() method of Math class returns the sum of the two arguments, throwing an exception if the result overflows a long or an int. Syntax public static int addExact (int x,...

1 minute read.