CoderSathi
  • Tutorial
    • Java Tutorial
    • Swing Tutorial
    • JDBC Tutorial
    • Java String Tutorial
    • Servlet and JSP Tutorial
  • Mongo DB
  • AWS
  • DevOps
  • Linux
  • Git
Home > Java > Difference between PreparedStatement and CallableStatement

Java Tutorial

  • Introduction
    • What is Java
    • History Of Java
    • Install Java
    • What is JVM
    • JDK vs JRE vs JVM
    • Java Bytecode
    • OOP vs POP
    • Compile and Run Java
  • Tokens, Expressions and Control Structures
    • Primitive data types
    • Integers
    • Floating Points
    • Characters
    • Booleans
    • User Defined Data Type
    • Declarations
    • Constants
    • Identifiers
    • Literals
    • Type Conversion and Casting
    • Variables
    • Default Variable Initialization
    • Command Line Arguments
    • Arrays of Primitive Types
    • Comment Syntax
    • Garbage Collection
    • Expressions
    • Operators
    • Arithmetic Operator
    • Bitwise and Shift Operator
    • Comparison or Relational Operators
    • Logical Operators
    • Assignment Operators
    • Ternary Operator
    • Increment and Decrement Operator
    • Control Statements
  • OOP Concepts
    • Class and Object
    • Create Class Instance
    • Method
    • Abstraction
    • Encapsulation
    • this keyword
    • Constructor
    • Pass by Value
    • Access Modifier/Control
    • Polymorphism
    • Method Overloading vs Method Overriding
    • Recursion
    • Nested and Inner Class
  • Inheritance and Packaging
    • Inheritance
    • extends Keyword
    • super Keyword
    • Object Class
    • Abstract class
    • Final Class
    • Java Package
    • Interface
  • Handling Error/ Exceptions
    • What is Exception
    • Exception Handling Keywords
    • Common Java Errors
    • User Defined Exception
    • Throwing and re-throwing Exception
    • finally Block
  • Strings
    • Java String Tutorial
  • Threads
    • Introduction
    • Create Thread
    • Thread Lifecycle
    • Thread Priority
    • Thread Synchronization
    • Inner Thread Communication
    • Thread Deadlock
  • IO and Streams
    • java.io Package
    • Files and Directories
    • Byte Stream
    • Character Stream
    • Console Input and Output
    • Serializable and Deserializable
  • Core Packages
    • java.lang Package
    • Math
    • Wrapper Classes
    • java.lang.Number
    • Double
    • Float
    • Integers
    • java.lang.Byte
    • java.lang.Short
    • java.lang.Long
    • java.lang.Character
    • java.lang.Boolean
    • java.util package
    • Vector Class
    • Stack Class
    • Dictionary Class
    • Hashtable
    • Enumeration or Enum
    • Generate Random Number
  • Holding Collection of Data
    • Arrays
    • Map
    • List
    • Set
    • Collection Interface
    • Collections Class
    • ArrayList
    • HashSet
    • TreeSet
    • Comparator
  • Java Bean
    • What is Java Bean
    • Advantages and Disadvantages of Java Bean
    • Java Beans API
    • Introspection
    • Java Bean Properties
    • Bound and Constrained Properties
    • BeanInfo Interface
    • Customizers
    • Java Beans Persistence
    • BeanDescriptor
  • Home

Difference between PreparedStatement and CallableStatement

Learn the concepts, implementation details, and practical steps with a clean developer-focused walkthrough.

Yuba Raj Kalathoki
By Yuba Raj Kalathoki
Last updated: July 1, 2026 ยท 3 min read ยท 0 Comments
Share: in X

PreparedStatement and CallableStatement are both interfaces in the Java Database Connectivity (JDBC) API that are used to execute SQL statements. They have some similarities but are designed for different purposes.

Table of Contents

  • Purpose
    • PreparedStatement
    • CallableStatement
  • Syntax
    • PreparedStatement
    • CallableStatement
    • Example of how to use IN parameters with CallableStatement:
    • Example of how to use OUT parameters with CallableStatement:
  • Execution
    • PreparedStatement
    • CallableStatement
  • Parameter handling
    • PreparedStatement
    • CallableStatement

Purpose

PreparedStatement

It is used to execute parameterized SQL statements. It allows you to precompile a SQL statement with placeholders for parameters, which can be set later with specific values. This is useful when you need to execute the same SQL statement multiple times with different parameter values.

CallableStatement

It is used to execute stored procedures or database functions. In addition to parameterized SQL statements, CallableStatement provides features to work with stored procedures, including input and output parameters.

Syntax

PreparedStatement

SQL statements executed with a PreparedStatement are typically regular SQL queries with placeholders for parameters. For example:

String sql = "SELECT * FROM customers WHERE age > ?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, 18);
ResultSet resultSet = statement.executeQuery();

CallableStatement

SQL statements executed with a CallableStatement are typically stored procedure calls or function calls using the appropriate database syntax. For example:

String sql = "{CALL procedureName(param1, param2)}";
CallableStatement statement = connection.prepareCall(sql);
statement.setInt(param1, value1);
//we can add as many parameter as we can.
statement.execute();
//Now we can print values from tatement

There are three types of parameters for CallableStatement: IN, OUT, and INOUT.

  • IN parameters are used to pass values to the stored procedure. The values of IN parameters are set before the stored procedure is executed.
  • OUT parameters are used to return values from the stored procedure. The values of OUT parameters are not set until the stored procedure has been executed.
  • INOUT parameters are a combination of IN and OUT parameters. The values of INOUT parameters are set before the stored procedure is executed, and they can also be updated by the stored procedure.

Example of how to use IN parameters with CallableStatement:

CallableStatement cs = connection.prepareCall("{call getEmployeeName(?)}");
cs.setString(1, "123456789");
ResultSet rs = cs.executeQuery();
while (rs.next()) {
    System.out.println(rs.getString(1));
}

In this example, the getEmployeeName() stored procedure takes one parameter, which is the employee ID. The setString() method is used to set the value of the parameter before the stored procedure is executed. The executeQuery() method is then used to execute the stored procedure and retrieve the results.

Example of how to use OUT parameters with CallableStatement:

CallableStatement cs = connection.prepareCall("{call getEmployeeNameOut(?)}");
cs.registerOutParameter(1, Types.VARCHAR);
cs.execute();
String employeeName = cs.getString(1);
System.out.println(employeeName);

In this example, the getEmployeeNameOut() stored procedure takes one parameter, which is an OUT parameter. The registerOutParameter() method is used to register the parameter before the stored procedure is executed. The execute() method is then used to execute the stored procedure. The value of the OUT parameter can be retrieved using the getString() method.

Execution

PreparedStatement

A PreparedStatement is executed using the executeQuery(), executeUpdate(), or execute() methods depending on the type of SQL statement. It is typically used for queries that retrieve data or perform data manipulation.

CallableStatement

A CallableStatement is executed using the execute() or executeUpdate() methods. It is used to call stored procedures or functions that can have both input and output parameters.

Parameter handling

PreparedStatement

You can set the values of parameters in a PreparedStatement using methods like setInt(), setString(), etc. The parameter values are bound to their corresponding placeholders in the SQL statement.

CallableStatement

In addition to setting input parameter values similar to PreparedStatement, CallableStatement allows you to register output parameters using methods like registerOutParameter(). These output parameters can be retrieved after the execution of the stored procedure or function.

The following table summarizes the key differences between PreparedStatement and CallableStatement:

PreparedStatementCallableStatement
Executes parameterized SQL queriesExecutes stored procedures
Can bind IN parameters at runtimeCan bind IN, OUT, and INOUT parameters at runtime
Can improve performanceCan improve performance even further
Can help prevent SQL injection attacksCannot prevent SQL injection attacks
Slower than CallableStatementFaster than PreparedStatement

Related Posts:

  • JDBC Interview Questions: Ace Your Technical Screening
  • Control Statements in Java
  • JDBC Tutorial: The Ultimate Guide to Java Database…
  • What is JDBC: Bridging Java and Databases
  • Executing Statements with JDBC
  • JDBC Configuration
Tags:javajdbc
Was this article helpful?
โ† Previous ArticlePrimitive data types in Java
Next Article โ†’Increase EBS volume size in AWS EC2

Leave a Comment Cancel reply

You must be logged in to post a comment.

Recent Posts

  • How to implement Passwordless Authentication in Spring Boot: A Step-by-Step Guide
  • How to Use AWS CloudFront Signed URLs in Spring Boot?
  • How to Fix SSH Agent Forwarding on macOS: The Ultimate Guide for Developers
  • How to Read AWS Secrets Manager in Spring Boot (Step-by-Step)
  • How to Fix “Public Key Retrieval is not allowed” MySQL JDBC Error
CoderSathi

Your go-to resource for Java, Spring Boot, Microservices, AWS, and modern development tutorials.

Linkedin

Quick Links

  • About
  • Contact

Popular Topics

  • Java
  • Spring Boot
  • AWS
  • DevOps
  • MongoDB
  • Linux
  • Git
  • How to
ยฉ 2026 CoderSathi. All rights reserved. Privacy Policy ยท Sitemap