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:

 * Copyright (c) All Rights Reserved.
import java.sql.*;
public class MySqlCallInoutParameter {
  public static void main(String [] args) {
    Connection con = null;
    try {
      com.mysql.cj.jdbc.MysqlDataSource ds
        = new com.mysql.cj.jdbc.MysqlDataSource();
      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;

// Execute the CALL statement and ignore result sets

// 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;
      fahrenheit = cs.getDouble(2);
      System.out.println("First test:");
      System.out.println("  Celsius = "+celsius);
      System.out.println("  Fahrenheit = "+fahrenheit);

// Close resource

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

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

herong> java -cp .:mysql-connector-java.jar \

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(
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(
   at com.mysql.jdbc.MysqlIO.checkErrorPacket(
   at com.mysql.jdbc.MysqlIO.sendCommand(
   at com.mysql.jdbc.MysqlIO.sqlQueryDirect(
   at com.mysql.jdbc.ConnectionImpl.execSQL(
   at com.mysql.jdbc.PreparedStatement.executeInternal(...)
   at com.mysql.jdbc.PreparedStatement.executeUpdate(...)
   at MySqlCallInoutParameter.main(

Table of Contents

 About This Book

 JDBC (Java Database Connectivity) Introduction

 JDK (Java SE) Installation

 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

 Using Connection Pool with JDBC

 Archived Tutorials


 Full Version in PDF/EPUB