English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

JDBC CallableStatement object example

The following example uses CallableStatement and the following getEmpName() MySQL stored procedure-

Make sure that this stored procedure has been created in the EMP database. You can use the MySQL query browser to do this.

DELIMITER $$
DROP PROCEDURE IF EXISTS `EMP`.`getEmpName` $$
CREATE PROCEDURE `EMP`.`getEmpName` 
   (IN EMP_ID INT, OUT EMP_FIRST VARCHAR(255))
BEGIN
   SELECT first INTO EMP_FIRST
   FROM Employees
   WHERE ID = EMP_ID;
END $$
DELIMITER ;

This example code is written based on the environment and database settings in the previous chapters.

Copy and paste the following example into JDBCExample.java, and compile and run as follows:

//Step1.Import the required software packages
import java.sql.*;
public class JDBCExample {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost/EMP";
   //  Database credentials
   static final String USER = "username";
   static final String PASS = "password";
   
   public static void main(String[] args) {
   Connection conn = null;
   CallableStatement stmt = null;
   try{
      //Step2: Register JDBC driver
      Class.forName("com.mysql.jdbc.Driver");
      //Step3: Establish connection
      System.out.println("Connecting to database...");
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      //Step4: Execute query
      System.out.println("Creating statement...");
      String sql = "{call getEmpName (?, ?)}";
      stmt = conn.prepareCall(sql);
      
      //First bind IN parameters, then bind OUT parameters
      int empID = 102;
      stmt.setInt(1, empID); // This will set the ID to102
      // Since the second parameter is OUT, register it
      stmt.registerOutParameter(2, java.sql.Types.VARCHAR);
      
      //Run the stored procedure using the execute method.
      System.out.println("Executing stored procedure...");
      stmt.execute();
      //Using getXXX method to retrieve employee name
      String empName = stmt.getString(2;
      System.out.println("Employee Name with ID:") + 
               empID + " is " + empName);
      catch(SQLException se
      conn.close();
   }catch(SQLException se){
      //Handling JDBC error
      se.printStackTrace();
   Handling Class.forName error
      //e.printStackTrace();
      }finally{
   Used to close resources
      //if(stmt!=null)
      try{
         stmt.close();
            catch(SQLException se
      }2{
      
      }
      try{
         if(conn!=null)
            conn.close();
      }catch(SQLException se){
         se.printStackTrace();
      }
   }
   System.out.println("Goodbye!");
}//End main
}//End JDBCExample

Now let's compile the above example as follows:

C:\>javac JDBCExample.java
C:\>

at runtimeJDBCExampleIt will produce the following result-

C:\>java JDBCExample
Connecting to database...
Creating statement...
Executing stored procedure...
Emp Name with ID:102 is Zaid
Goodbye!
C:\>