Calling MySQL Stored Procedures From JDBC
Calling MySQL Stored Procedures From JDBC
Home / MySQL JDBC Tutorial / Calling MySQL Stored Procedures from JDBC
?
In this tutorial, you will learn how to call MySQL stored procedures from JDBC using CallableStatement
object.
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
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:
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.
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
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();
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 }
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
Introducing to JDBC