← Back to Chapters

CallableStatement

? CallableStatement

? Quick Overview

CallableStatement is an interface in JDBC used to call stored procedures and functions defined in the database. It allows Java applications to execute complex SQL logic that runs directly on the database server.

? Key Concepts

  • Used for calling stored procedures
  • Supports IN, OUT, and INOUT parameters
  • Improves performance by executing logic at database level
  • Part of java.sql package

? Syntax / Theory

A CallableStatement is created using the prepareCall() method of the Connection interface.

? View Code Example
// Creating a CallableStatement to call a stored procedure
CallableStatement cs = con.prepareCall("{call procedure_name(?, ?)}");

? Interactive Visualizer

Simulate how IN parameters travel to the DB and OUT parameters travel back.

☕ Java App
(Source)
 
?️ Database
(Stored Proc)
Status: Ready to call procedure...

? Code Example

? View Code Example
// Calling a stored procedure with IN and OUT parameters
CallableStatement cs = con.prepareCall("{call getEmployeeName(?, ?)}");
cs.setInt(1, 101);
cs.registerOutParameter(2, java.sql.Types.VARCHAR);
cs.execute();
String name = cs.getString(2);

? Live Output / Explanation

?️ Output

The stored procedure is executed in the database. The OUT parameter returns the employee name, which is retrieved using getString() in Java.

? Tips & Best Practices

  • Always register OUT parameters before executing the statement
  • Use stored procedures for repeated and complex database logic
  • Close CallableStatement objects to avoid resource leaks

? Try It Yourself

  • Create a stored procedure that returns total salary of an employee
  • Call the procedure using CallableStatement
  • Experiment with INOUT parameters