CallableStatement in JDBC Example | prepareCall() Example in JDBC

PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
JAVAEXCEPTIONSCOLLECTIONSSWINGJDBC
EXCEPTIONS
COLLECTIONS
SWING
JDBC
JAVA 8
SPRING
SPRING BOOT
HIBERNATE
PYTHON
PHP
JQUERY
PROGRAMMINGJava ExamplesC Examples
Java Examples
C Examples
C Tutorials
aws
In this tutorial, we are going to learn bout CallableStatement in JDBC. CallableStatement in JDBC is an interface, which is coming from the java.sql package. We have already discussed in the previous tutorial Steps by Step JDBC program example, statements in JDBC are 3 types. Here we are going to discuss one of the type called CallableStatement.
CallableStatement in JDBC is a sub interface of PreparedStatement.
CallableStatement in JDBC has all the benefits of PreparedStatement and also it has one more additional feature, that is we can call the procedures or functions of a database.
CallableStatement is only for calling a procedure or a function of a database. But it is not for creating a procedure or function.
CallableStatement has two syntaxes, one is for executing commands and another is for calling the procedure or function.
We can get the CallableStatement object by calling the prepareCall() method on connection object.
CallableStatement cstmt = connection.prepareCall("sql command");
CallableStatement cstmt = connection.prepareCall("{call procedure(args)}");
CallableStatement cstmt = connection.prepareCall("{?=call function(args)}");
Creating Procedure in MySql :
mysql> delimiter $
mysql> create procedure square(IN a int, OUT b int)
-> begin
-> set b=a*a;
-> end;
-> $
Example:
package com.onlinetutorialspoint.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;

public class JDBC_Procedures_Example {

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root",
                "123456");
        CallableStatement cStmt = connection.prepareCall("{call square(?,?)}");
        cStmt.setInt(1, 20);
        cStmt.registerOutParameter(2, Types.INTEGER);
        cStmt.execute();
        System.out.println("The Square is : " + cStmt.getInt(2));
        cStmt.close();
    }

}
In the above code, we register the out parameter to CallableStatement by using the registerOutParameter(). It is mandatory because if we do not register the “out” parameter, then by default CallableStatement will take all the parameters as “in” parameters. So to inform CallableStatement that it is an out parameter we need to  register it.
While registering the out parameter, we need to tell the CallableStatement about the parameter type also. CallableStatement can understand only JDBC Types, but not java or database types. In JDBC Types class contains all the JDBC data types. Types is a class in java.sql package.
Output :
The Square is : 400
Creating Function in MySQL :
mysql> DELIMITER $
mysql> CREATE FUNCTION mul(a int, b int) RETURNS INT
-> BEGIN
-> DECLARE c INT;
-> SET c = a*b;
-> RETURN c;
-> END;
-> $
Example:
package com.onlinetutorialspoint.jdbc;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Types;

class JDBC_Function_Example {

    public static void main(String[] args) throws Exception {
        Connection connection = null;
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection(
                "jdbc:mysql://localhost:3306/onlinetutorialspoint", "root",
                "123456");
        CallableStatement cStmt = connection.prepareCall("{?=call mul(?,?)}");

        cStmt.registerOutParameter(1, Types.INTEGER);
        cStmt.setInt(2, 20);
        cStmt.setInt(3, 60);
        cStmt.execute();
        System.out.println("The Multiplication is : " + cStmt.getInt(1));
        cStmt.close();
    }
}
Output:
The Multiplication is : 1200
Happy Learning 🙂

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example

JDBC Interview Questions and Answers
JDBC Select Program Example
JDBC Insert Program Example
JDBC Update Program Example
JDBC Delete Program Example
JDBC PreparedStatement Example Program
Insert an Image using JDBC in Mysql DB
Read an Image in JDBC Example
ResultSetMetaData in JDBC Example
DatabaseMetaData in JDBC Example
Transaction Management in JDBC Example
Batch Processing in JDBC Example
JDBC Connection with Properties file
JDBC Scrollable ResultSet Example
JDBC Updatable ResultSet Example
 


Δ
 JDBC Driver Types
 Step by Step JDBC Program
 JDBC Select Program
 JDBC Insert Program
 JDBC Update Program
 JDBC Delete Program
 JDBC Connection – Properties File
 JDBC PreparedStatement Program
 JDBC – CallableStatement Example
 JDBC – Read an Image from DB
 JDBC – Insert an Image in DB
 JDBC – Updatable ResultSet
 JDBC – Scrollable ResultSet
 JDBC – ResultSetMetaData
 JDBC – DatabaseMetaData
 JDBC – Transaction Management
 JDBC – Batch Processing
 JDBC Interview Questions