CallableStatement

All Java Topics
Last updated: May 25, 2026
Author: ManaCoding Team

CallableStatement in Java JDBC is used to call stored procedures and functions in a database like MySQL or Oracle. It allows executing precompiled SQL logic stored in the database.

📝 Syntax
CallableStatement cs = con.prepareCall("{call procedure_name(?, ?)}");
cs.setInt(1, value);
cs.setString(2, value);
cs.execute();
💻 Example Program
import java.sql.*;

public class Main {

  public static void main(String[] args) {

    try {

      Connection con = DriverManager.getConnection(
        "jdbc:mysql://localhost:3306/college",
        "root",
        "password"
      );

      // Calling stored procedure
      CallableStatement cs = con.prepareCall("{call getStudentById(?)}");

      cs.setInt(1, 1);

      ResultSet rs = cs.executeQuery();

      while (rs.next()) {
        System.out.println(rs.getInt("id") + " " + rs.getString("name"));
      }

      con.close();

    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}

// Output:
// 1 John
💡 What is CallableStatement?
  • 1 Used to execute stored procedures.
  • 2 Part of java.sql package.
  • 3 Extends PreparedStatement.
  • 4 Used for database-side logic execution.
💡 Why Use CallableStatement?
  • 1 Improves performance with stored procedures.
  • 2 Enhances security.
  • 3 Reduces SQL complexity in Java code.
  • 4 Centralizes business logic in DB.
💡 Types of Calls
  • 1 Stored procedures.
  • 2 Functions returning values.
  • 3 Procedures with input/output parameters.
  • 4 Batch database operations.
💡 CallableStatement vs PreparedStatement
  • 1 CallableStatement calls stored procedures.
  • 2 PreparedStatement executes SQL queries.
  • 3 CallableStatement is used for DB logic.
  • 4 PreparedStatement is used for CRUD operations.
Quick Summary
  • CallableStatement is used to call stored procedures.
  • It is part of JDBC API.
  • It improves performance and security.
  • Used in enterprise applications.
FAQs
What is CallableStatement in JDBC?
It is used to execute stored procedures in the database.
Difference between CallableStatement and PreparedStatement?
CallableStatement is used for stored procedures, PreparedStatement for SQL queries.
Which method is used to call stored procedure?
prepareCall()
Which package contains CallableStatement?
java.sql package.
Why use stored procedures?
For performance, security, and centralized logic.
🎯 Interview Questions
Q1. What is CallableStatement in JDBC?
Answer: It is used to execute stored procedures in the database.
Q2. Difference between CallableStatement and PreparedStatement?
Answer: CallableStatement is used for stored procedures, PreparedStatement for SQL queries.
Q3. Which method is used to call stored procedure?
Answer: prepareCall()
Q4. Which package contains CallableStatement?
Answer: java.sql package.
Q5. Why use stored procedures?
Answer: For performance, security, and centralized logic.
Quiz

What is CallableStatement used for?