Creating CallableStatement Objects with INOUT Parameters

This section describes how to create CallableStatement objects with INOUT parameters.

MySQL procedures do support INOUT parameters, which should be handled like IN parameters and OUT parameters as shown in the previous tutorial.

In a previous tutorial, I defined a store procedure called, C2F(), with 2 INOUT parameters. The program below shows you to create a CallableStatement object to execute this stored procedure:

/* MySqlCallInoutParameter.java
 - Copyright (c) 2015, HerongYang.com, All Rights Reserved.
 */
import java.sql.*;
public class MySqlCallInoutParameter {
  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 C2F(?,?)");

// Register INOUT parameters
      cs.registerOutParameter(1, java.sql.Types.REAL);     
      cs.registerOutParameter(2, java.sql.Types.REAL);     

// Provide values for INOUT parameters
      double celsius = 100.0;
      cs.setDouble(1,celsius);

// Execute the CALL statement and ignore result sets
      cs.executeUpdate();

// Retrieve values from INOUT parameters
      double fahrenheit = cs.getDouble(2);
      System.out.println("First test:");
      System.out.println("  Celsius = "+celsius);
      System.out.println("  Fahrenheit = "+fahrenheit);

// Second test
      celsius = 0.0;
      cs.setDouble(1,celsius);
      cs.executeUpdate();
      fahrenheit = cs.getDouble(2);
      System.out.println("First test:");
      System.out.println("  Celsius = "+celsius);
      System.out.println("  Fahrenheit = "+fahrenheit);

// Close resource
      cs.close();

      con.close();
    } catch (Exception e) {
      System.err.println("Exception: "+e.getMessage());
      e.printStackTrace();
    }
  }
}

The execution gave me one surprise. The second test failed with an error that I don' understand:

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\javac 
   -cp .;\local\lib\mysql-connector-java-5.1.36-bin.jar 
   MySqlCallInoutParameter.java

C:\herong>\Progra~1\java\jdk1.8.0_45\bin\java 
   -cp .;\local\lib\mysql-connector-java-5.1.36-bin.jar 
   MySqlCallInoutParameter

First test:
  Celsius = 100.0
  Fahrenheit = 212.0
  
Exception: Data truncated for column 'F' at row 1
java.sql.SQLException: Data truncated for column 'F' at row 1
   at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:998)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3847)
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3783)
   at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2447)
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2594)
   at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
   at com.mysql.jdbc.PreparedStatement.executeInternal(...)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(...)
   ...
   at MySqlCallInoutParameter.main(MySqlCallInoutParameter.java:41)  

Last update: 2015.

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 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 - sqljdbc42.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

 Outdated Tutorials

 References

 PDF Printing Version