Statement vs CallableStatement in JDBC | Generated by AI

Home 2025.09

In Java’s JDBC (and other frameworks that build on it, like Hibernate or Spring), the concepts of Statement and CallableStatement are both interfaces used to send SQL commands to the database, but they serve different purposes.


1. Statement

Example:

Connection conn = DriverManager.getConnection(url, user, pass);
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT id, name FROM employees");

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

2. CallableStatement

Example:

Connection conn = DriverManager.getConnection(url, user, pass);
CallableStatement cstmt = conn.prepareCall("{call getEmployeeName(?, ?)}");

// Set IN parameter
cstmt.setInt(1, 101);

// Register OUT parameter
cstmt.registerOutParameter(2, java.sql.Types.VARCHAR);

// Execute
cstmt.execute();

// Get OUT parameter
String empName = cstmt.getString(2);
System.out.println("Employee name: " + empName);

Key Differences (without a table)


👉 If you want, I can also explain how PreparedStatement fits between these two (safer, parameterized SQL, but not for stored procedures). Would you like me to add that?


Back Donate