CallableStatement
CallableStatement is used for calling stored procedures in a database. It can also execute dynamic SQL statements.
It extends PreparedStatement and provides additional methods for working with stored procedures.
Suitable for executing database functions and procedures, especially in the context of database transactions.
Example using CallableStatement (Java):
javaCopy codeimport java.sql.Connection;
import java.sql.DriverManager;
import java.sql.CallableStatement;
import java.sql.ResultSet;
public class CallableStatementExample {
public static void main(String[] args) {
// Connection setup code here
try (Connection connection = DriverManager.getConnection(jdbcUrl, username, password)) {
String procedureCall = "{CALL get_users_by_age(?)}";
int minAge = 18;
try (CallableStatement callableStatement = connection.prepareCall(procedureCall)) {
callableStatement.setInt(1, minAge);
ResultSet resultSet = callableStatement.executeQuery();
while (resultSet.next()) {
// Process the results
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
Explanation:
CallableStatementExample: Uses
CallableStatement
to execute a stored procedureget_users_by_age
with a specified minimum age parameter. CallableStatement is ideal for executing stored procedures and functions.
Last updated