Creating CallableStatement Objects with prepareCall()
<< MySQL - JBDC CallableStatement
<< JDBC Tutorials - Herong's Tutorial Notes
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) 2007 by Dr. Herong Yang. All rights reserved. */ import java.sql.*; public class MySqlCallableStatement { public static void main(String [] args) { Connection con = null; try { com.mysql.jdbc.jdbc2.optional.MysqlDataSource ds = new com.mysql.jdbc.jdbc2.optional.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(); } } }
Compilation and execution went ok. There was no output from the program as expected:
C:\>javac -cp .;\local\lib\mysql-connector-java-5.0.7-bin.jar MySqlCallableStatement.java C:\java -cp .;\local\lib\mysql-connector-java-5.0.7-bin.jar MySqlCallableStatement
Sections in This Chapter
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
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