Spring boot JDBC example

In this post, we will see Spring boot JDBC example.

As we already know Spring boot does lot of auto configurations which help us to avoid a lot of boilerplate code. In the case of JDBC, spring boot provides auto-configuration such as creating DataSource and JDBCTemplate objects automatically based on application.properties.

So you just need to autowire JdbcTemplate bean in your application code as below.

Let’s create a simple Spring boot JDBC example.

Tools used

  • Spring Boot 2.2.2.RELEASE
  • Spring JDBC 5.2.2.RELEASE
  • HikariCP 3.4.1
  • Maven 3
  • Java 8

Project Structure


Create new Spring boot project

Step 1:  Go to https://start.spring.io and create a project with following dependencies

  • spring-boot-starter-jdbc

Here is the screenshot for the same.


Maven configuration

Add Mysql drive dependency
We need to add MySQL JDBC drive dependency in order to connect to Mysql.

Your pom.xml will look like below:

Configure datasource in Application.properties

Please change your username and password as per your mysql local setup.

Create database table

Let’s create a database table named Students which we are going to use in this example.

studentName varchar(255) DEFAULT NULL,
age int(3) DEFAULT NULL,

Create model class

Let’s create simple Student.java.

Create Repository interface and implementation

Create its implemntation named StudentRepositoryImpl.java

We have use Spring’s JDBCTemplate to interact with database. JDBCTemplate helps us to avoid boiler plate code and provides convenient methods to retrieve and insert data in database.

Create service interface and implementation

Create a service interface StudentService.java

Create its implementation named StudentRepositoryImpl.java

Please note that we have annotated StudentServiceImpl with @Service. Spring will automatically create a bean based on this annotation.

StudentRepository is being injected into StudentServiceImpl class and StudentServiceImpl is delegating all CRUD operatons to StudentRepository which actually interacts with database with Spring JDBCTemplate.

Create Application class

we have injected StudentService class in SpringBootJdbcExampleApplication and use StudentService to do the CRUD operation here.

In testStudentData(), we have create 4 student objects and performed CRUD operation with the help of StudentService methods.

Run the application

When you will run the application, you will get below output:

[SAVE] Saving student with name: John
2019-12-25 00:09:09.183 INFO 18336 — [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Starting…
2019-12-25 00:09:10.190 INFO 18336 — [ main] com.zaxxer.hikari.HikariDataSource : HikariPool-1 – Start completed.
Saving student with name: Martin
Saving student with name: Mary
Saving student with name: Ricky
get All students: [Student [studentId=1, StudentName=John,age =16], Student [studentId=2, StudentName=Martin,age =18], Student [studentId=3, StudentName=Mary,age =20], Student [studentId=4, StudentName=Ricky,age =15]] Find Student with id 2
Student with id 2: Student [studentId=2, StudentName=Martin,age =18] Update age of Martin to 19
Rows affected: 1
Delete Student with id 4
Rows affected: 1
get updated list of Students: [Student [studentId=1, StudentName=John,age =16], Student [studentId=2, StudentName=Martin,age =19], Student [studentId=3, StudentName=Mary,age =20]]

Verify database output

Let’s run the query to datbase and check if our changes are reflected in Students table

As you can see, tables shows updated data after performing CRUD operations.

Github Source code

That’s all about Spring boot JDBC example.


You may also like:

Related Posts

  • 02 January

    Spring boot devtools tutorial

    Table of ContentsOverviewJRebelIntroduction to Spring Boot DevToolsAdding Devtools to your projectSpring Boot DevTools FeaturesProperty DefaultsLoggingCachingAutomatic RestartLive ReloadGlobal SettingsRemote ApplicationsFile System WatchersConclusion In this post, we’ll be looking up what does spring has to offer for developer experience. Overview Using IDEs like Eclipse, Spring Tool Suite, Intellij Idea a typical development setup includes an application code […]

  • 26 December

    Spring boot H2 Database example

    Table of ContentsWhat is H2 database?Tools usedGithub Source code:Project StructureCreate new Spring boot projectMaven configurationH2 database configurationOverride H2 databse defaultsModelRepositoryServiceControllerSpring boot main fileRun the applicationTest the applicationCreate movie objectGet all movie objectsDelete movie id 3 In this post, we are going to see how to create Spring boot application integrating with H2 database. What is […]

  • 22 December

    Spring Boot CrudRepository

    Table of ContentsTools usedProject Structure:Wiring up MongoDBThe Repository ClassThe Save methodThe User Entity ClassBringing the UserDao and CrudRepository togetherServiceControllerApplication classRunning the code sampleConclusionSource code In this post, we’ll be looking up how to create and use Spring Boot CrudRepository. SpringBoot CrudRepository provides sophisticated CRUD functionality for the type of entity you want to be managed.This […]

  • 10 April

    Difference between Spring and Spring boot

    Table of ContentsSpring frameworkSpring bootAdvantages of Spring BootSpring vs Spring boot In this post, we will see difference between Spring and Spring boot. If you have followed spring project over time, you might have noticed it has become increasingly complex. If you want to create a spring application, you still need to put a lot […]

  • 17 September

    Spring Boot SOAP Web service Example

    Table of ContentsTools usedProject structureAdd wsdl4j dependencyXSD file to define domainGenerate domain classes based on XSDCreate Book repository with dummy dataDefine Book service endpointDefine web service configurationRun the applicatonTest WSDL URLTest Spring soap web serviceSource code In this post, we will see how to create soap web services with Spring boot. We will create contract […]

  • 08 August

    Spring boot profiles example

    Table of ContentsNeed for profilesProfiles for environment specific propertiesSetting active profileSpring boot @Profile annotationProject StructureSource code In this tutorial, we will see about Spring boot profiles. Need for profiles Application development is complex process. You might have different environment for application development. Dev SIT QA Prod We require different configuration based on the environment. For […]

Leave a Reply

Your email address will not be published.

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.