EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials Spring Tutorial Spring Boot JdbcTemplate Example
 

Spring Boot JdbcTemplate Example

Updated March 13, 2023

spring boot jdbctemplate example

 

 

Introduction to Spring Boot JdbcTemplate Example

Spring boot jdbctemplate example is used to create a connection between application API and database server. Spring boot jdbc template is a mechanism that was used to connect the database server and execute the query on a database server. Spring boot template internally uses the API of JDBC, but it has problems with JDBC API. Therefore, using the spring boot template, we do not need to handle the transaction.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Steps by steps Spring Boot JdbcTemplate Example

Below is the step-by-step procedure to create the example of the spring boot JDBC template is as follows.

  • Create the database in the PostgreSQL database server –

In this step, we are creating the database name as spring_boot_jdbctemplate in the PostgreSQL database server.

Code –

# create database spring_boot_jdbctemplate;
# \l+ spring_boot_jdbctemplate

spring boot jdbctemplate example output 1

  • Create the table into spring_boot_jdbctemplate database –

In this step, we are creating the table name as a stud in the database name as spring_boot_jdbctemplate.

Code –

# create table stud (id int, f_name varchar(10));

spring boot jdbctemplate example output 2

  • Create project template using spring initializer and give a name to project –

 In the below step, we have provided project group name as com.example, artifact name as springbootjdbctemplate, project name as springbootjdbctemplate, and selected java version as 8.

 Group – com.example                        Artifact name – springbootjdbctemplate

Name – springbootjdbctemplate       Description – Project for springbootjdbctemplate

Spring boot – 2.6.0                              Project – Maven project

Java – 8                                               Dependencies – spring web, PostgreSQL driver

Package name – com.example.springbootjdbctemplate

spring boot jdbctemplate example output 3

  • After generating project extract files and open this project by using spring tool suite –

After generating the project using the spring initializer in this step, we are extracting the jar file; after extracting the jar file, we are opening the project template using the spring tool suite.

spring boot jdbctemplate example output 4

  • After opening the project using the spring tool suite, check the project and its files –

In this step, we are checking all the project template files. We also need to check the maven dependencies and system libraries.

spring boot jdbctemplate example output 5

  • Add dependency packages –

 In this step, we are adding the required dependency in our project. In this example, we are adding the spring boot starter web, spring boot starter test, and PostgreSQL dependency in the spring boot jdbc template example project. 

Code –

<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-maven-plugin</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-test</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>org.springframework.boot</groupId>   -- Start and end of groupId tag.
<artifactId>spring-boot-starter-web</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.
<dependency>   -- Start of dependency tag.
<groupId>org.postgresql</groupId>   -- Start and end of groupId tag.
<artifactId>postgresql</artifactId>  -- Start and end of artifactId tag.
</dependency>    -- End of dependency tag.

output 6

  • Create stud class –

After adding the required dependency next step is to create a class of our project. In this class, we have defined the two properties with the getter and setter methods.

Code –

public class stud
{
private int id;
private String f_name;
public stud() {}
public int getId () {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getF_name () {
return f_name;
}
public void setF_name (String f_name) {
this.f_name = f_name;
}
public stud(int id, String f_name) {
super();
this.id = id;
this.f_name = f_name;
}
}

output 7

  • Create studDao.java class –

After creating the stud class, we have created the studDao.java class for our spring boot jdbctemplate example project.

This class contains the jdbc template property and three methods name as saveStud(), updateStud() and deleteStud().

Code –

public class studDao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate (JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public int saveStud (stud s) {
String dbquery = "insert into stud values ('" + s.getId () + "','" + s.getF_name () + "')";
return jdbcTemplate.update(dbquery);
}
public int updateStud (stud s) {
String dbquery = "update stud set f_name='" + s.getF_name () + "' where id='" + s.getId () + "' ";
return jdbcTemplate.update(dbquery);
}
public int deleteStud (stud s) {
String dbquery = "delete from stud where id='" + s.getId () + "' ";
return jdbcTemplate.update(dbquery);
}
}

output 8

  • Create applicationContext.xml file –

After creating stud and studDao classes next step is to create an applicationContext.xml file for the spring boot jdbc template example project.

Code –

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="postgresql.jdbc.driver.postgresqlDriver" />
<property name="url" value="jdbc:postgresql:@localhost:5432:spring_boot_jdbctemplate" />
<property name="username" value="postgres" />
<property name="password" value="postgres" />
</bean>

output 9

  • Test the application by running the saveStud() method –

In the below example, we tested our spring boot jdbc template example project using the saveStud method.

Code –

public class jdbctemplateTest {
public static void main(String[] args) {
ApplicationContext ctx = new ClassPathXmlApplicationContext ("applicationContext.xml");
studDao sd = (studDao)ctx.getBean("springbootjdbctemplate");
int status = sd.saveStud (new stud(101,"ABC"));
System.out.println (status);
}
}

output 10

  • We can also check the updateStud() and deleteStud() method by using the following code are as follows.

1) updateStud () –

int status = sd.updateStud (new stud(101,"PQR"));
System.out.println (status);

2) deleteStud () –

stud s = new stud();
s.setId (101);
int status = sd.deleteStud (s);
System.out.println (status);
  • Run the application –

In this step, we are running our spring boot jdbc template example project using the spring tool suite.

output 11

  • After running the application, check data is inserted into a database table –

In this step, we are checking the data is inserted into the database table. We can see that data is inserted into the stud table.

Code –

# select * from stud;

output 12

Recommended Articles

This is a guide to the Spring Boot JdbcTemplate Example. Here we discuss the step-by-step procedure to create the example of the Spring Boot JdbcTemplate. You may also have a look at the following articles to learn more –

  1. Spring Boot Logging
  2. Spring Boot cors
  3. Spring Boot HTTPS
  4. Spring Boot Banner

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW