🚀 Master Java database programming, JDBC, and MySQL stored procedures!
🔔 Subscribe to Ram N Java on YouTubeCalling MySQL Stored Procedures with JDBC CallableStatement
In MySQL, stored procedures are precompiled collections of SQL queries stored directly inside the database catalog. Using stored procedures improves performance, centralizes business logic, and helps secure database operations.
In Java, the CallableStatement interface provides a standard mechanism to invoke stored procedures and retrieve the resulting ResultSet directly into your application.
The MySQL Stored Procedure (get_all_cities)
Below is the stored procedure created in the MySQL world database. When called, it executes a query returning all records from the city table:
DELIMITER $$
CREATE PROCEDURE `get_all_cities`()
BEGIN
SELECT * FROM city;
END$$
DELIMITER ;
Complete Java Source Code Example
The following Java program loads the MySQL JDBC driver, establishes a connection, executes the stored procedure using prepareCall(), and prints the result set:
package com.ram.jdbc;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
public class JDBCCallableStatementDemo {
static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
static final String DB_URL = "jdbc:mysql://localhost:3306/world";
static final String USER = "root";
static final String PASS = "root";
public static void main(String[] args) {
JDBCCallableStatementDemo demo = new JDBCCallableStatementDemo();
demo.getAllCitiesInfo();
}
public void getAllCitiesInfo() {
Connection conn = null;
CallableStatement cstmt = null;
ResultSet rs = null;
try {
// Step 1: Register MySQL JDBC Driver
Class.forName(JDBC_DRIVER);
// Step 2: Establish database connection
conn = DriverManager.getConnection(DB_URL, USER, PASS);
// Step 3: Prepare the stored procedure call
String sql = "{call get_all_cities()}";
cstmt = conn.prepareCall(sql);
// Step 4: Execute the procedure & retrieve the ResultSet
rs = cstmt.executeQuery();
System.out.println("ID | Name | CountryCode | District | Population");
System.out.println("-------------------------------------------------");
while (rs.next()) {
int id = rs.getInt("ID");
String name = rs.getString("Name");
String countryCode = rs.getString("CountryCode");
String district = rs.getString("District");
int population = rs.getInt("Population");
System.out.println(id + " | " + name + " | " + countryCode + " | " + district + " | " + population);
}
} catch (ClassNotFoundException | SQLException e) {
e.printStackTrace();
} finally {
// Step 5: Clean up database resources
try {
if (rs != null) rs.close();
if (cstmt != null) cstmt.close();
if (conn != null) conn.close();
} catch (SQLException se) {
se.printStackTrace();
}
}
}
}
Key Steps to Remember
-
Use
conn.prepareCall("{call get_all_cities()}")with JDBC escape syntax to invoke the procedure. -
Because the procedure executes a
SELECTquery directly, callcstmt.executeQuery()to obtain the resultingResultSetdirectly. -
Iterate through the result rows using
while (rs.next())and standard getter methods. -
Always close the
ResultSet,CallableStatement, andConnectionin thefinallyblock to avoid resource leaks.
Related Tutorials You Might Enjoy
Click here to watch in Youtube : https://www.youtube.com/watch?v=iC7J6EFhjZQ&list=UUhwKlOVR041tngjerWxVccw&index=5
Click the below Image to Enlarge
![]() |
| JDBC - CallableStatement (Mysql) |
![]() |
| JDBC - CallableStatement (Mysql) |
.jpg)
.jpg)
No comments:
Post a Comment