JDBC Tutorials - Herong's Tutorial Examples - Version 2.21, by Dr. Herong Yang

Creating Procedures with Multiple Queries

This section describes how to create a procedure with multiple queries.

To do more tests on CallableStatement objects, I created another stored procedure with multiple queries:

C:\>\local\mysql\bin\mysql -u Herong -pTopSecret

mysql> USE HerongDB;
Database changed

mysql> -- Changing the command delimiter to '$'
mysql> DELIMITER '$';

mysql> -- Creating the procedure
mysql> CREATE PROCEDURE HeadTail(OUT Size INTEGER)
    -> BEGIN
    ->   SELECT ID, FirstName, LastName, BirthDate, ModTime
    ->     FROM Profile ORDER BY ID LIMIT 0, 10;
    ->   SELECT ID, FirstName, LastName, BirthDate, ModTime
    ->     FROM Profile ORDER BY ID DESC LIMIT 0, 10;
    ->   SELECT COUNT(*) INTO Size FROM Profile;
    -> END$
Query OK, 0 rows affected (0.00 sec)

mysql> -- Testing the procedure
mysql> CALL HeadTail(@Size)$
+-------+-----------+----------+------------+---------------------+
| ID    | FirstName | LastName | BirthDate  | ModTime             |
+-------+-----------+----------+------------+---------------------+
| 80029 | 2265      | 9e559    | 1988-12-31 | 2006-12-31 23:59:59 |
| 80030 | 378       | 48189    | NULL       | 2007-04-01 21:35:14 |
| 80031 | 1a46      | 108f3    | NULL       | 2007-04-01 21:35:14 |
| 80032 | 44f       | c8511    | NULL       | 2007-04-01 21:35:14 |
| 80033 | 1e9a      | 51ca6    | NULL       | 2007-04-01 21:35:14 |
| 80034 | 665       | 6eb12    | NULL       | 2007-04-01 21:35:14 |
| 80035 | 1b97      | 30e6d    | NULL       | 2007-04-01 21:35:14 |
| 80036 | 3d8       | 270d6    | NULL       | 2007-04-01 21:35:14 |
| 80037 | 26a3      | b2290    | NULL       | 2007-04-01 21:35:14 |
| 80038 | 1d65      | d9d6d    | NULL       | 2007-04-01 21:35:14 |
+-------+-----------+----------+------------+---------------------+
10 rows in set (0.00 sec)

+-------+-----------+----------+------------+---------------------+
| ID    | FirstName | LastName | BirthDate  | ModTime             |
+-------+-----------+----------+------------+---------------------+
| 90029 | Herong    | Yang     | 1988-12-31 | 2006-12-31 23:59:59 |
| 90028 | 545       | 728d4    | NULL       | 2007-04-01 21:35:17 |
| 90027 | 169b      | 1f61d    | NULL       | 2007-04-01 21:35:17 |
| 90026 | 2559      | 737b4    | NULL       | 2007-04-01 21:35:17 |
| 90025 | 1db2      | 48b31    | NULL       | 2007-04-01 21:35:17 |
| 90024 | 149       | cdf71    | NULL       | 2007-04-01 21:35:17 |
| 90023 | 12a5      | 8a909    | NULL       | 2007-04-01 21:35:17 |
| 90022 | 1b67      | c6f96    | NULL       | 2007-04-01 21:35:17 |
| 90021 | 12a9      | 20ac8    | NULL       | 2007-04-01 21:35:17 |
| 90020 | 18a4      | 1ffb4    | NULL       | 2007-08-31 21:35:17 |
+-------+-----------+----------+------------+---------------------+
10 rows in set (0.03 sec)

Query OK, 0 rows affected (0.06 sec)

mysql> -- Check the output parameter
mysql> SELECT @Size$
+-------+
| @Size |
+-------+
| 10001 |
+-------+
1 row in set (0.00 sec)

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 Downloading and Installing JDK - Java SE

 Installing and Running Java DB - Derby

 Derby (Java DB) JDBC Driver

 Derby (Java DB) JDBC DataSource Objects

 Java DB (Derby) - DML Statements

 Java DB (Derby) - ResultSet Objects of Queries

 Java DB (Derby) - PreparedStatement

 MySQL Installation on Windows

 MySQL JDBC Driver (MySQL Connector/J)

 MySQL - PreparedStatement

 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

 Oracle Express Edition Installation on Windows

 Oracle JDBC Drivers

 Oracle - Reference Implementation of JdbcRowSet

 Oracle - PreparedStatement

 Oracle - JBDC CallableStatement

 Oracle CLOB (Character Large Object) - TEXT

 Oracle BLOB (Binary Large Object) - BLOB

 Microsoft SQL Server 2005 Express Edition

 Microsoft JDBC Driver for SQL Server - sqljdbc.jar

 Microsoft JDBC Driver - Query Statements and Result Sets

 Microsoft JDBC Driver - DatabaseMetaData Object

 Microsoft JDBC Driver - DDL Statements

 Microsoft JDBC Driver - DML Statements

 SQL Server - PreparedStatement

 SQL Server CLOB (Character Large Object) - TEXT

 SQL Server BLOB (Binary Large Object) - BLOB

 JDBC-ODBC Bridge Driver - sun.jdbc.odbc.JdbcOdbcDriver

 JDBC-ODBC Bridge Driver - Flat Text Files

 JDBC-ODBC Bridge Driver - MS Access

 JDBC-ODBC Bridge Driver - MS SQL Server

 Summary of JDBC Drivers and Database Servers

 Additional Tutorial Notes to Be Added

 References

 PDF Printing Version

Creating Procedures with Multiple Queries - Updated in 2014, by Dr. Herong Yang