JDBC for MySQL - Herong's Tutorial Examples - v3.12, by Herong Yang
Creating CallableStatement Objects with prepareCall()
This section describes how to create a CallableStatement object with the prepareCall() method.
The first test I did was to create a CallableStatement with the prepareCall() method to call my simple procedure, HelloWorld():
/* MySqlCallableStatement.java * Copyright (c) HerongYang.com. All Rights Reserved. */ import java.sql.*; public class MySqlCallableStatement { public static void main(String [] args) { Connection con = null; try { com.mysql.cj.jdbc.MysqlDataSource ds = new com.mysql.cj.jdbc.MysqlDataSource(); ds.setServerName("localhost"); ds.setPortNumber(3306); ds.setDatabaseName("HerongDB"); ds.setUser("Herong"); ds.setPassword("TopSecret"); con = ds.getConnection(); // Create CallableStatement CallableStatement cs = con.prepareCall("CALL HelloWorld()"); // Execute the call statement cs.executeUpdate(); // Close resource cs.close(); con.close(); } catch (Exception e) { System.err.println("Exception: "+e.getMessage()); e.printStackTrace(); } } }
Let's compile and run the program in one command. There is no output from the program as expected:
herong> java -cp .:mysql-connector-java.jar \ MySqlCallableStatement.java
See the next tutorial on how to collect the returning ResultSet objects from the execution of a CallableStatement.
Table of Contents
JDBC (Java Database Connectivity) Introduction
MySQL JDBC Driver (MySQL Connector/J)
MySQL - Reference Implementation of JdbcRowSet
►MySQL - JBDC CallableStatement
Overview of CallableStatement Objects
"CREATE PROCEDURE" - Creating a Simple Procedure
Creating Procedures with IN and OUT Parameters
Creating Procedures with INOUT Parameters
Creating Procedures with Multiple Queries
►Creating CallableStatement Objects with prepareCall()
Capturing ResultSet with executeQuery()
Creating CallableStatement Objects with Parameters
Common Errors with CallableStatement Parameters
Creating CallableStatement Objects with INOUT Parameters
Retrieving Multiple ResultSet Objects
Executing Stored Procedures without Permission
getProcedures() - Listing Stored Procedures
MySQL CLOB (Character Large Object) - TEXT
MySQL BLOB (Binary Large Object) - BLOB