How to call stored procedures in the Spring Framework?

By | June 24, 2021

In this post, We will learn about How to call stored procedures in the Spring Framework using a Demo Project.

The org.springframework.jdbc.core.simple.SimpleJdbcCall class is mainly used to call a stored procedure with IN and OUT parameters. We may use this approach while working with either of the databases such as MySQLApache Derby, DB2, Microsoft SQL Server, Oracle, and Sybase etc.

 

pom.xml

We need to add Spring and MySQL dependencies in Maven pom.xml file.

Employee.java

Employee model class to populate employee data

EmployeeService.java

Employee Service Interface

EmployeeServiceImpl.java

Employee Service Implementation class

EmployeeDao.java

Employee DAO(Data Access Object) Interface

EmployeeDaoImpl.java

Employee DAO(Data Access Object) Interface implementation class to call Stored Procedure in MySQL database.

Spring org.springframework.jdbc.core.simple.SimpleJdbcCall  is the central class in the Spring JDBC core package and it provides a method to call Stored Procedures.

applicationContext.xml

The org.springframework.jdbc.datasource.DriverManagerDataSource class is mainly used to contain information about the database such as driver class name, connection URL, username, and password.

There is a property Spring bean with the name as datasource in the org.springframework.jdbc.core.simple.SimpleJdbcCall class of org.springframework.jdbc.datasource.DriverManagerDataSource type. So, we need to provide the reference of the DriverManagerDataSource object in the SimpleJdbcCall class for the data source property.

Here, we are using the SimpleJdbcCall object in the EmployeeDaoImpl class, so we are passing it by the setter injection but you can also use constructor injection.

DB.sql

Here we are going to use the below database script to create employee_table and insert one record in the MySQL database.

MySQL Database Screen Shot After running DB.sql

StoredProcedure.sql

Let’s create one Stored Procedure in MySQL. This Stored Procedure takes emp_id as input and returns the corresponding employee’s name,email,gender and salary using OUT parameters. Let us create this stored procedure in the test database.

Screen Shot After Creating Stored Procedure in MySQL test Schema

ClientTest.java

This ClientTest class with the main method gets the bean from the applicationContext.xml file and the calls method getEmployeeById of EmployeeService to get employee details by calling Stored Procedure in MySQL database.

If you run ClientTest.java as Java Application then it will give the below output: 

That’s all about How to call stored procedures in the Spring Framework?

You May Also Like:

Spring BeanFactory Container Example
Spring ApplicationContext Container Example
Annotation-based Configuration in Spring Framework Example
Spring Java-based Configuration Example
Spring Setter Dependency Injection Example
Spring @Autowired Annotation With Setter Injection Example
Spring Constructor based Dependency Injection Example
Spring @Autowired Annotation With Constructor Injection Example
Spring Autowiring byName & byType Example
getBean() overloaded methods in Spring Framework
Spring Inner bean example
Spring Dependency Injection with Factory Method
Spring Framework @Qualifier example
Injecting Collections in Spring Framework Example
Spring Bean Definition Inheritance Example
Spring bean scopes with example
Spring JSR-250 Annotations with Example
Spring BeanPostProcessor Example
Spring JDBC Integration Example
Spring JDBC Annotation Example
Spring with Jdbc java based configuration example
Spring JDBC NamedParameterJdbcTemplate Example

If you have any feedback or suggestion please feel free to drop in below comment box.

Leave a Reply

Your email address will not be published.