Spring Boot makes working with stored procedures using JdbcTemplate very easy.
Below is a Step-By-Step Guide:
- Step 1:
Let's create our simple stored procedure.
CREATE PROCEDURE get_employee_name (IN emp_id INT, OUT emp_name VARCHAR(50)) BEGIN SELECT employeeName INTO emp_name FROM employee WHERE employeeId = emp_id; END
Our stored procedure named get_employee_name takes an employee ID as input and returns the corresponding employee name as output.
- Step 2:
Let's create our POJO class to map the stored procedure parameters and outputs. In our example, we need a class with two fields: empId and empName.
public class EmployeeName { private int empId; private String empName; // getters and setters }
- Step 3:
Create a CallableStatementCreator to execute our stored procedure. This class is responsible for creating the CallableStatement object that will call the stored procedure.
public class EmployeeNameCallableStatementCreator implements CallableStatementCreator { private final int empId; public EmployeeNameCallableStatementCreator(int empId) { this.empId = empId; } @Override public CallableStatement createCallableStatement(Connection connection) throws SQLException { CallableStatement cs = connection.prepareCall("{call get_employee_name(?, ?)}"); cs.setInt(1, empId); cs.registerOutParameter(2, Types.VARCHAR); return cs; } }
- Step 4:
Crete our @Repository class.
@Repository public class EmployeeRepository { private final JdbcTemplate jdbcTemplate; public EmployeeRepository(JdbcTemplate jdbcTemplate) { this.jdbcTemplate = jdbcTemplate; } public String getEmployeeName(int empId) { EmployeeNameCallableStatementCreator callableStatementCreator = new EmployeeNameCallableStatementCreator(empId); EmployeeName employeeName = jdbcTemplate.execute(callableStatementCreator, (CallableStatementCallback<EmployeeName>) cs -> { cs.execute(); EmployeeName result = new EmployeeName(); result.setEmpId(empId); result.setEmpName(cs.getString(2)); return result; }); return employeeName.getEmpName(); } }
- Step 5:
Finally, we can call the getEmployeeName() method from your service or controller class to get the employee name for a given employee ID.
@RestController @RequestMapping("/employee") public class EmployeeController { private final EmployeeRepository employeeRepository; public EmployeeController(EmployeeRepository employeeRepository) { this.employeeRepository = employeeRepository; } @GetMapping("/{empId}/name") public String getEmployeeName(@PathVariable int empId) { return employeeRepository.getEmployeeName(empId); } }
Facing issues? Have Questions? Post them here! I am happy to answer!
- Deep Dive into Java 8 Predicate Interface
- Read and Parse XML file using Java DOM Parser [Java Tutorial]
- Java 8 Predicate Functional Interface isEqual() Method Example
- Convert Multidimensional Array toString In Java
- How to read int value using Scanner Class Java
- Spring Boot AI + LLM + Java Code Example
- Write to a File using Java Stream API
- Implementing Bubble Sort Algorithm using Java Program
- How to Fix XmlBeanDefinitionStoreException in Java SpringBoot ApplicationConfig.xml
- YAML Parser using Java Jackson Library Example
- [Fix] java: integer number too large compilation error
- Convert JSON String to Java GSON Object Example
- Read a file using Java 8 Stream
- Java Spring Boot 3 Web Hello World with Gradle in IntelliJ
- Ways Compare Dates in Java Programming with Examples
- Pretty Print JSON String in Java Console Output
- Java JDBC with Join Queries Example
- How to Check For Updates on Windows 11 (Step-by-Step)
- [Fix] java.net.MalformedURLException: unknown protocol
- How to display date and time in GMT Timezone in Java
- Error: LinkageError occurred while loading main class UnsupportedClassVersionError [Eclipse Java]
- How to convert a String to Java 8 Stream of Char?
- RabbitMQ Queue Listener Java Spring Boot Code Example
- 5+ Fibonacci number Series Java Program Examples [ 0 1 1 2 3 ..]
- Handling NullPointerException with Java Predicate
- macOS Ventura: In order to use Xcode.app you need to update to the latest version - MacOS
- Commodity Future and Options Month Code Calculator - Tools
- Changing Android Intent Tittle using java code - Android
- How to know my IP on Mac Ventura 13.0 - MacOS
- Delete a Directory using Mac Terminal Command - MacOS
- Center align text in TextView Android Programming - Android
- How to Clone Bitbucket repository to Local Computer - Git
- Change Mac Terminal Font Size using Command - MacOS