Spring JdbcTemplate Delete Example

Hey guys in this post, we will discuss deleting a record from the database using JdbcTemplate in Spring boot application.


  • JdbcTemplate is class which will help us to query the database
  • update()is a method provided by JdbcTemplate, which is used to delete a single record from the database
  • update() takes 2 parameters, sql_query and parameter

Complete Example

We will create this example step by step, follow this tutorial till the end

Read More:

Create a Database

Open the mysql console or workbench and execute the following queries

CREATE DATABASE springbootjdbc;

USE springbootjdbc;

CREATE TABLE tbl_employees
    name VARCHAR(255) NOT NULL,
    location VARCHAR(255) NOT NULL,

INSERT INTO tbl_employees
(name, location, dateOfBirth)
("Bushan", "India", now());

INSERT INTO tbl_employees
(name, location, dateOfBirth)
("Bharath", "India", now());

Create spring boot project

There are many different ways to create a spring boot application, you can follow the below articles to create one –

>> Create spring boot application using Spring initializer
>> Create spring boot application in Spring tool suite [STS]
>> Create spring boot application in IntelliJ IDEA

Add maven dependencies

Open pom.xml and add the following dependencies –

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
		<relativePath/> <!-- lookup parent from repository -->
	<description>Spring boot jdbc project</description>




spring-boot-starter-web dependency for building web applications using Spring MVC. It uses the tomcat as the default embedded container.

spring-boot-devtools dependency for automatic reloads or live reload of applications. spring-boot-starter-jdbc is a starter dependency for communicating with the database. lombok dependency is a java library that will reduce the boilerplate code that we usually write inside every entity class like setters, getters, and toString(). mysql-connector-java dependency is to store the data into the database.

Configure the datasource

Open application.properties file and add the following contents


Create an entity class

Create Employee.java inside the in.bushansirgur.springboot.entity package and add the following content

package in.bushansirgur.springbootjdbc.entity;

import java.util.Date;

import lombok.AllArgsConstructor;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

public class Employee {
	private Long id;
	private String name;
	private String location;
	private Date dateOfBirth;
	public Employee(String name, String location, Date dateOfBirth) {
		this.name = name;
		this.location = location;
		this.dateOfBirth = dateOfBirth;
  • @Setter annotation generates the setter methods
  • @Getter annotation generates the getter methods
  • @AllArgsConstructor generates parameterised constructor with all the fields
  • @NoArgsConstructor generates no-arg constructor

Create a DAO

Create an interface EmployeeDAO.java inside the in.bushansirgur.springboot.dao package and add the following content

package in.bushansirgur.springbootjdbc.DAO;

import java.util.List;

import in.bushansirgur.springbootjdbc.entity.Employee;

public interface EmployeeDAO {
	public List<Employee> findAll();
	public Employee findById(int id);
	public int deleteById(int id);

Create EmployeeDAOImpl.java inside the in.bushansirgur.springboot.dao package and add the following content

package in.bushansirgur.springbootjdbc.DAO;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import in.bushansirgur.springbootjdbc.entity.Employee;

public class EmployeeDAOImpl implements EmployeeDAO{

	JdbcTemplate jdbcTemplate;
	public List<Employee> findAll() {
		return jdbcTemplate.query("SELECT * FROM tbl_employees", new BeanPropertyRowMapper<Employee>(Employee.class));

	public Employee findById(int id) {
		return jdbcTemplate.queryForObject("SELECT * FROM tbl_employees WHERE id=?", new BeanPropertyRowMapper<Employee>(Employee.class), id);

	public int deleteById(int id) {
		return jdbcTemplate.update("DELETE FROM tbl_employees WHERE id=?", id);

Create a Controller

Create EmployeeController.java inside the in.bushansirgur.springbootjdbc.controller package and add the following content

package in.bushansirgur.springbootjdbc.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import in.bushansirgur.springbootjdbc.DAO.EmployeeDAO;
import in.bushansirgur.springbootjdbc.entity.Employee;

public class EmployeeController {
	private EmployeeDAO eDAO;
	public List<Employee> findAll() {
		return eDAO.findAll();
	public Employee findById(@PathVariable int id) {
		return eDAO.findById(id);
	public String deleteById(@PathVariable int id) {
		return eDAO.deleteById(id)+" Employee(s) delete from the database";

Run the app

Run the application using the below maven command –

mvn spring-boot:run

Open the Browser or Postman and enter the following URL –

  • localhost:8080/employees


  • localhost:8080/employees/2


  • localhost:8080/employees/3


That’s it for this post. I hope you enjoyed this post, if you did then please share this with your friends and colleagues. Also, you can share this post with your social media profiles. Thank you I will see you in the next post.

Bushan Sirgur

Hey guys, I am Bushan Sirgur from Banglore, India. Currently, I am working as an Associate project in an IT company.

Leave a Reply