Execute Stored Procedure in Spring Jdbc

Execute Stored Procedure in Spring Jdbc thumbnail
28K
By Dhiraj 04 February, 2017

Spring jdbc provides very simple approach to execute stored procedures using SimpleJdbcCall.In this post we will be dicussing about how to execute stored proc in spring spring jdbc using SimpleJdbcCall.

Server Side

Follwing is a sample user class which will be persisted in the DB by calling a stored proc.

User.java
package com.devglan.model;

public class User {

    private int id;
    private String name;
    private String address;
    private String email;
	
    public int getId() {
	return id;
    }

    public void setId(int id) {
	this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }

}
 Other Interesting Posts
Spring 5 Features and Enhancements
Insert Record in DB with Spring Boot JdbcTemplate
Insert Record in DB with Spring Boot Namedparameter Jdbctemplate
Fetch Auto Generated Primary Key Value After Insert Spring Jdbc
Working with Spring Boot Jdbctemplate
Working with Spring Boot NamedParameter Jdbctemplate
Spring Security Hibernate Example with complete JavaConfig
Securing REST API with Spring Security Basic Authentication
Websocket spring Boot Integration without STOMP with complete JavaConfig
Spring Boot Spring MVC Example
Spring Boot Thymeleaf Example
Spring Boot MVC with Jsp Example

Following is the class having an implementation to execute stored proc.Following method createUser() takes user object as a parameter and executes the stored proc create_user.

package com.devglan.dao.impl;

import java.util.Map;

import javax.sql.DataSource;

import org.springframework.beans.factory.annotation.Autowired;
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.devglan.dao.UserDao;
import com.devglan.model.User;

@Repository
public class UserDaoImpl implements UserDao {

    private SimpleJdbcCall createUserProc;
	
    @Autowired
    public void setDataSource(DataSource dataSource) {
       this.createUserProc = new SimpleJdbcCall(dataSource).withProcedureName("create_user").withReturnValue();
    }
    
    @Override
    public User createUser(User user){
    	String userId = null;
    	SqlParameterSource inParams = new MapSqlParameterSource()
				.addValue("name", user.getName())
				.addValue("email", user.getEmail())
				.addValue("address", user.getAddress());
    	
		Map result = createUserProc.execute(inParams);
		userId = (String) result.get("id");
		user.setId(userId);
		return user;
	}
	
}

Conclusion

I hope this article served you that you were looking for. If you have anything that you want to add or share then please share it below in the comment section.

Share

If You Appreciate This, You Can Consider:

We are thankful for your never ending support.

About The Author

author-image
A technology savvy professional with an exceptional capacity to analyze, solve problems and multi-task. Technical expertise in highly scalable distributed systems, self-healing systems, and service-oriented architecture. Technical Skills: Java/J2EE, Spring, Hibernate, Reactive Programming, Microservices, Hystrix, Rest APIs, Java 8, Kafka, Kibana, Elasticsearch, etc.

Further Reading on Spring Jdbc