Call Stored Procedure using Spring SimpleJdbcCall

This tutorial will show you how we can call or execute stored procedure using Spring SimpleJdbcCall. SimpleJdbcCall API provides flexible methods to invoke stored procedure. We can use DataSource directly with SimpleJdbcCall or we can create JdbcTemplate using DataSource and use JdbcTemplate with SimpleJdbcCall API for calling the stored procedures.

We can also use RowMapper or BeanProprtyMapper of type POJO class as a return type while executing stored procedures using Spring’s SimpleJdbcCall API. We are also going to use @Qualifier annotation for resolving ambiguity while using SimpleJdbcCall with DataSource and JdbcTemplate.

Related Posts:

Sometimes we need to call a stored procedure while a single SQL statement is unable to fulfil our requirements because, it permits to execute multiple SQL statements and it also allows us to execute these SQL statements in a transactional unit in an efficient manner to avoid any inconsistency sometimes occurs during the execution of multiple SQL statements.


Eclipse 2019-12, At least JDK 1.8, Maven 3.6.3, Gradle 6.4.1, Spring Boot 2.3.1

Setup Project

You need to create a project in Eclipse. The name of the project is spring-spring-stored-procedure-simplejdbccall.

If you are creating gradle based project then use below build.gradle script.

buildscript {
	ext {
		springBootVersion = '2.3.1.RELEASE'
    repositories {
    dependencies {

plugins {
    id 'java-library'
    id 'org.springframework.boot' version "${springBootVersion}"

sourceCompatibility = 12
targetCompatibility = 12

repositories {

dependencies {
	//required for jdk 9 or above

If you are creating maven based project then use below pom.xml file:

<project xmlns=""




		<!--required only if jdk 9 or higher version is used-->

					<source>at least 8</source>
					<target>at least 8</target>

Database Configuration

In order to use database we need to configure database with Spring Boot application. We will create src/main/resources/ file with the below configurations.


We need to create corresponding Java configuration class for creating different beans. We have the same bean types SimpleJdbcCall, so we have given name to each of them to resolve ambiguity.

package com.roytuts.spring.stored.procedure.simplejdbccall.config;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;

public class DbConfig {

	private Environment environment;

	public DataSource dataSource() {
		return DataSourceBuilder.create().driverClassName(environment.getProperty("spring.datasource.driverClassName"))

	public JdbcTemplate jdbcTemplate(DataSource dataSource) {
		return new JdbcTemplate(dataSource);

	@Bean(name = "simpleJdbcCall1")
	public SimpleJdbcCall simpleJdbcCall1(DataSource dataSource) {
		return new SimpleJdbcCall(dataSource);

	@Bean(name = "simpleJdbcCall2")
	public SimpleJdbcCall simpleJdbcCall2(JdbcTemplate jdbcTemplate) {
		return new SimpleJdbcCall(jdbcTemplate);


Create Model

Model class is a plain old Java object which will represent a row in the table.

package com.roytuts.spring.stored.procedure.simplejdbccall.model;

public class Student {

	private Integer studentId;
	private String studentName;
	private String studentDob;
	private String studentEmail;
	private String studentAddress;

	public Student() {

	public Student(Integer studentId, String studentName, String studentDob, String studentEmail,
			String studentAddress) {
		this.studentId = studentId;
		this.studentName = studentName;
		this.studentDob = studentDob;
		this.studentEmail = studentEmail;
		this.studentAddress = studentAddress;

	//getters and setters


Mapper Class

We are using Spring JDBC API, so we need a row mapper class that will map each column of the table with the Java field.

The column name you will see later when we will talk about table creation.

package com.roytuts.spring.stored.procedure.simplejdbccall.rowmapper;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.springframework.jdbc.core.RowMapper;

import com.roytuts.spring.stored.procedure.simplejdbccall.model.Student;

public class StudentRowMapper implements RowMapper<Student> {

	public Student mapRow(ResultSet rs, int rowNum) throws SQLException {
		Student student = new Student();
		return student;


Call Stored Procedures

Next we will call two stored procedures using Spring SimpleJdbcCall API. One stored procedure will fetch single Student details and another stored procedure will fetch all Students details.

The below class is a DAO or Repository class that interacts with the underlying database.

Notice we have used both SimpleJdbcCall API beans to fetch the single student record.

We have also used RowMapper class (StudentRowMapper) or BeanPropertyRowMapper class to map the rows from table into Java List<Student> class.

package com.roytuts.spring.stored.procedure.simplejdbccall.dao;

import java.sql.Types;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.SqlOutParameter;
import org.springframework.jdbc.core.SqlParameter;
import org.springframework.jdbc.core.namedparam.MapSqlParameterSource;
import org.springframework.jdbc.core.namedparam.SqlParameterSource;
import org.springframework.jdbc.core.simple.SimpleJdbcCall;
import org.springframework.stereotype.Repository;

import com.roytuts.spring.stored.procedure.simplejdbccall.model.Student;
import com.roytuts.spring.stored.procedure.simplejdbccall.rowmapper.StudentRowMapper;

public class StudentDao {

	private SimpleJdbcCall simpleJdbcCall1;

	private SimpleJdbcCall simpleJdbcCall2;

	private final String DB_NAME = "roytuts";
	private final String PROC_GET_STUDENT = "get_student";
	private final String PROC_GET_ALL_STUDENTS = "get_all_students";

	public Student getStudentUsingProcMethod1(int studentId) {
		// schema name
		// procedure name
		// in parameter, must be same as in the procedure
		// Map with key/value pair also be passed to addValue() function
		SqlParameterSource sqlParameterSource = new MapSqlParameterSource().addValue("in_student_id", studentId);
		// get result in key/value pair
		Map<String, Object> result = simpleJdbcCall1.execute(sqlParameterSource);
		// get Student object
		Student student = new Student();
		// simple the studentId
		// set Name, must be same as out param in procedure
		student.setStudentName((String) result.get("out_student_name"));
		// set Date of Birth, must be same as out param in procedure
		student.setStudentDob((String) result.get("out_student_dob"));
		// set Email, must be same as out param in procedure
		student.setStudentEmail((String) result.get("out_student_email"));
		// set Address, must be same as out param in procedure
		student.setStudentAddress((String) result.get("out_student_address"));
		return student;

	public Student getStudentUsingProcMethod2(int studentId) {
		// schema name
		// procedure name
		// You can opt to declare one, some, or all the parameters explicitly
		// in parameter, must be same as in the procedure
		// in and out parameters, must be same as in the procedure
		simpleJdbcCall2.declareParameters(new SqlParameter("in_student_id", Types.INTEGER),
				new SqlOutParameter("out_student_name", Types.VARCHAR),
				new SqlOutParameter("out_student_dob", Types.VARCHAR),
				new SqlOutParameter("out_student_email", Types.VARCHAR),
				new SqlOutParameter("out_student_address", Types.VARCHAR));
		// put in parameter value in Map
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("in_student_id", studentId);
		// execute the procedure
		// get result in key/value pair
		Map<String, Object> result = simpleJdbcCall2.execute(map);
		// get Student object
		Student student = new Student();
		// simple the studentId
		// set Name, must be same as out param in procedure
		student.setStudentName((String) result.get("out_student_name"));
		// set Date of Birth, must be same as out param in procedure
		student.setStudentDob((String) result.get("out_student_dob"));
		// set Email, must be same as out param in procedure
		student.setStudentEmail((String) result.get("out_student_email"));
		// set Address, must be same as out param in procedure
		student.setStudentAddress((String) result.get("out_student_address"));
		return student;

	public List<Student> getAllStudentUsingProcMethod1() {
		// schema name

		// procedure name, BeanPropertyRowMapper or RowMapper

		// simpleJdbcCall1.withProcedureName(PROC_GET_ALL_STUDENTS).returningResultSet("students",
		// BeanPropertyRowMapper.newInstance(Student.class));

		simpleJdbcCall1.withProcedureName(PROC_GET_ALL_STUDENTS).returningResultSet("students", new StudentRowMapper());

		// get results in key/value pair
		Map<String, Object> results = simpleJdbcCall1.execute();

		List<Student> students = (List<Student>) results.get("students");

		return students;


Create MySQL Table

Create a table called student under roytuts database in MySQL server.

CREATE TABLE `student` (
  `student_id` int unsigned NOT NULL AUTO_INCREMENT,
  `student_name` varchar(30) NOT NULL,
  `student_dob` varchar(10) NOT NULL,
  `student_email` varchar(80) NOT NULL,
  `student_address` varchar(250) NOT NULL,
  PRIMARY KEY (`student_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;

To test our application right away we need some data. So we are going to dump some sample data into the above table.

insert  into `student`(`student_id`,`student_name`,`student_dob`,`student_email`,`student_address`) values (1,'Sumit','01-01-1980','','Garifa'),
(3,'Debina','01-01-1982','','Salt Lake'),
(5,'Liton','01-01-1990','','Salt Lake');

Create Stored Procedures

We also need to create stored procedures to fetch student details. We will create two separate stored procedure in the MySQL server under roytuts database.

The below procedure fetches single student record:

    PROCEDURE `roytuts`.`get_student`(IN in_student_id INTEGER,
	    OUT out_student_name VARCHAR(30),
	    OUT out_student_dob VARCHAR(10),
	    OUT out_student_email VARCHAR(80),
	    OUT out_student_address VARCHAR(255))
	SELECT student_name, student_dob, student_email, student_address
	INTO out_student_name, out_student_dob, out_student_email, out_student_address
	FROM student WHERE student_id = in_student_id;

You can execute or call the above stored procedure in MySQL server in the following way:

CALL get_student(1,@name,@dob,@email,@address)

Now get the result by executing below SELECT statement:

SELECT @name,@dob,@email,@address


@name	@dob	    @email	          @address
Sumit	01-01-1980	  Garifa

The below procedure fetches all students from the table:

    PROCEDURE `roytuts`.`get_all_students`()
	SELECT * FROM student;

Execute using CALL get_all_students() command.


spring stored procedure simplejdbccall

Spring REST Controller

We would like to expose two endpoints to get the results of the stored procedures.


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.roytuts.spring.stored.procedure.simplejdbccall.dao.StudentDao;
import com.roytuts.spring.stored.procedure.simplejdbccall.model.Student;

public class StudentRestController {

	private StudentDao dao;

	public ResponseEntity<Student> getStudent(@PathVariable Integer id) {
		System.out.println("id: " + id);
		// Student student = dao.getStudentUsingProcMethod1(id);
		Student student = dao.getStudentUsingProcMethod2(id);
		return new ResponseEntity<Student>(student, HttpStatus.OK);

	public ResponseEntity<List<Student>> getAllStudents() {
		List<Student> students = dao.getAllStudentUsingProcMethod1();

		return new ResponseEntity<List<Student>>(students, HttpStatus.OK);


Main Class

A class with main method and @SpringBootApplication is enough for deploying Spring Boot application into Tomcat server.

package com.roytuts.spring.stored.procedure.simplejdbccall;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication(scanBasePackages = "com.roytuts.spring.stored.procedure.simplejdbccall")
public class SpringStoredProcedureSimpleJdbcCallApp {

	public static void main(String[] args) {, args);


Testing the Application

Now when your application is up and running you can hit the following two URLs to get the results of the stored procedures.

URL: http://localhost:8080/student/1



URL: http://localhost:8080/students


[{"studentId":1,"studentName":"Sumit","studentDob":"01-01-1980","studentEmail":"","studentAddress":"Garifa"},{"studentId":2,"studentName":"Gourab","studentDob":"01-01-1982","studentEmail":"","studentAddress":"Garia"},{"studentId":3,"studentName":"Debina","studentDob":"01-01-1982","studentEmail":"","studentAddress":"Salt Lake"},{"studentId":4,"studentName":"Souvik","studentDob":"01-01-1992","studentEmail":"","studentAddress":"Alipore"},{"studentId":5,"studentName":"Liton","studentDob":"01-01-1990","studentEmail":"","studentAddress":"Salt Lake"}]

Source Code


Thanks for reading.

0 Thoughts to “Call Stored Procedure using Spring SimpleJdbcCall”

  1. Filip

    I am an absolute newbie in Java but I’d like to learn. Accidentally I ended up in project with maven, spring and few other things. And THIS is the first tutorial that actually WORKS and gives me results when I follow instructions. Finally I UNDERSTAND how this whole thing works. Thanks a lot!

Leave a Comment