0% found this document useful (0 votes)
54 views

Calling MySQL Stored Procedures From JDBC

This document discusses how to call MySQL stored procedures from Java using JDBC. It introduces the CallableStatement object for calling stored procedures and shows the syntax. An example stored procedure that returns candidate skills is created. The example then demonstrates how to call this stored procedure by preparing a CallableStatement, setting parameters, executing the query, and traversing the result set. The full code sample illustrates the complete process of connecting to MySQL and calling a stored procedure from JDBC.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views

Calling MySQL Stored Procedures From JDBC

This document discusses how to call MySQL stored procedures from Java using JDBC. It introduces the CallableStatement object for calling stored procedures and shows the syntax. An example stored procedure that returns candidate skills is created. The example then demonstrates how to call this stored procedure by preparing a CallableStatement, setting parameters, executing the query, and traversing the result set. The full code sample illustrates the complete process of connecting to MySQL and calling a stored procedure from JDBC.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

21/11/2018 Calling MySQL Stored Procedures from JDBC

HOME BASIC ADVANCED  FUNCTIONS  INTERFACES  TIPS TRYIT

Home / MySQL JDBC Tutorial / Calling MySQL Stored Procedures from JDBC

Calling MySQL Stored Procedures from JDBC

?
In this tutorial, you will learn how to call MySQL stored procedures from JDBC using CallableStatement
object.

Before you start


For the sake of demonstration, we will create a new stored procedure named get_candidate_skill
that accepts candidate_id as the IN parameter and returns a result set that contains the skills of the
candidate.

1 DELIMITER $$
2 CREATE PROCEDURE get_candidate_skill(IN candidate_id INT)
3 BEGIN
4 SELECT candidates.id, first_name,last_name, skills.name AS skill
5 FROM candidates
6 INNER JOIN candidate_skills ON candidates.id = candidate_skills.candidate_id
7 INNER JOIN skills ON skills.id = candidate_skills.skill_id
8 WHERE candidates.id = candidate_id;
9     END$$
10 DELIMITER ;

Let’s call this stored procedure for candidate id with value 122.

1 CALL get_candidate_skill(122);

http://www.mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc/ 1/8
21/11/2018 Calling MySQL Stored Procedures from JDBC

HOME BASIC ADVANCED  FUNCTIONS  INTERFACES  TIPS TRYIT

Introducing to CallableStatement and stored procedure call syntax


To call stored procedures or stored functions in MySQL from JDBC, you use CallableStatement object,
which inherits from PreparedStatement object. The general syntax of calling a stored procedure is as
follows:

1 {?= call procedure_name(param1,param2,...)}

You wrap the stored procedure call within braces ({}). If the stored procedure returns a value, you need
to add the question mark and equal (?=) before the call keyword. If a stored procedure does not return
any values, you just omit the ?= sign. In case the stored procedure accepts any parameters, you list them
within the opening and closing parentheses after the stored procedure’s name.

The following are examples of using the syntax for calling stored procedures in di erent contexts:

Syntax Stores Procedures

{  call procedure_name() } Accept no parameters and return no value

{ call procedure_name(?,?) } Accept two parameters and return no value

{?= call procedure_name() } Accept no parameter and return value

{?= call procedure_name(?) } Accept one parameter and return value

Notice that question mark placeholder (?) can be used for both IN ,OUT, and INOUT parameters. For
detailed information on di erent parameter types in stored procedures, check it out MySQL stored
procedure parameters tutorial.

JDBC MySQL stored procedure example


First, open a connection to MySQL server by creating a new Connection object.

1 Connection conn = DriverManager.getConnection();

Then, prepare a stored procedure call and create a CallableStatement object by calling prepareCall()
method of the Connection object.

http://www.mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc/ 2/8
21/11/2018 Calling MySQL Stored Procedures from JDBC

1 String query = "{CALL get_candidate_skill(?)}";


2 CallableStatement stmt = conn.prepareCall(query)

Next, pass all the parameters to the stored procedure. In this case, the get_candidate_skill stored
HOME BASIC ADVANCED  FUNCTIONS  INTERFACES  TIPS TRYIT
procedure accepts only one IN parameter.

1 stmt.setInt(1, candidateId);

After that, execute the stored procedure by calling the executeQuery() method of the
CallableStatement object. It returns a result set in this case.

1 ResultSet rs = stmt.executeQuery();

Finally, traverse the ResultSet to display the results.

1 while (rs.next()) {
2    System.out.println(String.format("%s - %s",
3                       rs.getString("first_name") + " "
4                       + rs.getString("last_name"),
5                       rs.getString("skill")));
6 }

The following is the complete example of calling the MySQL stored procedure from JDBC.

1 package org.mysqltutorial;
2  
3 import java.sql.Connection;
4 import java.sql.ResultSet;
5 import java.sql.SQLException;
6 import java.sql.CallableStatement;
7  
8 /**
9 *
10 * @author mysqltutorial.org
11 */
12 public class Main {
13  
14     /**
15      * Get skills by candidate id
16      *
17      * @param candidateId
18      */
19     public static void getSkills(int candidateId) {
20         //
21         String query = "{ call get_candidate_skill(?) }";
22         ResultSet rs;
23  
24         try (Connection conn = MySQLJDBCUtil.getConnection();
25                 CallableStatement stmt = conn.prepareCall(query)) { ⤒
26  

http://www.mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc/ 3/8
21/11/2018 Calling MySQL Stored Procedures from JDBC
27             stmt.setInt(1, candidateId);
28  
29             rs = stmt.executeQuery();
30             while (rs.next()) {
31                 System.out.println(String.format("%s - %s",
32 HOME BASIC ADVANCED  FUNCTIONS+ " "
                        rs.getString("first_name") INTERFACES  TIPS TRYIT
33                         + rs.getString("last_name"),
34                         rs.getString("skill")));
35             }
36         } catch (SQLException ex) {
37             System.out.println(ex.getMessage());
38         }
39     }
40  
41     /**
42      *
43      * @param args
44      */
45     public static void main(String[] args) {
46         getSkills(122);
47     }
48 }

Let’s run the program.

The program works as expected.

In this tutorial, we have shown you how to call a stored procedure in MySQL database from a Java
program using JDBC CallableStatement object.

Related Tutorials
Connecting to MySQL Using JDBC Driver

Querying Data From MySQL Using JDBC

Updating Data in MySQL Using JDBC PreparedStatement

Inserting Data Into Table Using JDBC PreparedStatement

MySQL JDBC Transaction

Introducing to JDBC

Setting Up MySQL JDBC Development Environment ⤒


Writing and Reading MySQL BLOB Using JDBC
http://www.mysqltutorial.org/calling-mysql-stored-procedures-from-jdbc/ 4/8

You might also like