EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 600+ Courses All in One Bundle
  • Login
Home Software Development Software Development Tutorials Spring Tutorial spring boot jdbc
Secondary Sidebar
Spring Tutorial
  • Spring Boot
    • What is Spring Boot
    • Spring Boot flyway
    • Spring Boot framework
    • Spring Boot Logback
    • Spring Boot actuator endpoints
    • Spring Boot gRPC
    • Spring Boot jdbctemplate example
    • Spring Boot ehcache
    • Spring Boot Architecture
    • Spring Boot Port
    • Introduction of spring boot
    • Spring Boot ide
    • Spring Boot Netty
    • Spring Boot ORM
    • Spring Boot Versions
    • Spring Boot JUnit
    • Spring Boot Keycloak
    • Spring Boot gradle
    • Spring Boot Lombok
    • Spring Boot autowired
    • Spring Boot bean
    • Spring Boot hibernate
    • Spring Boot integration test
    • Spring Boot jdbc
    • Spring Boot MongoDB
    • Spring Boot postgresql
    • Spring Boot rest
    • Spring Boot swagger
    • Spring Boot thymeleaf
    • Spring Boot Unit Test
    • Spring Boot Webflux
    • Spring Boot webclient
    • Spring Boot kubernetes
    • Spring Boot Properties
    • Spring Boot Validation
    • Spring Boot Feature
    • Spring Boot Application
    • Spring Boot email
    • Spring Boot MVC
    • Spring Boot Exception Handling
    • Spring Boot Starter Parent
    • Spring Boot Docker
    • Spring Boot Logging
    • Spring Boot Query
    • Spring Boot Multiple Data Sources
    • Spring Boot Basic Authentication
    • Spring Boot Test
    • Spring Boot jwt
    • Spring Boot Liquibase
    • Spring Boot Prometheus
    • Spring Boot debug
    • Spring Boot GraalVM
    • Spring Boot Batch
    • Spring Boot controller
    • Spring Boot CLI
    • Spring Boot file upload
    • Spring Boot interceptor
    • Spring Boot Service
    • Spring Boot Configuration
    • Spring Boot Datasource Configuration
    • Spring Boot Annotations
    • Spring Boot Starter We
    • Spring Boot Actuator
    • Spring Boot DevTools
    • Spring Boot Repository
    • Spring Boot Dependencies
    • Spring Boot Path Variable
    • Spring Boot Microservices
    • Spring Boot Run Command
    • Spring Boot application.properties
    • Spring Boot Transaction Management
    • Spring Boot Banner
    • Spring Boot JPA
    • Spring Boot Change Port
    • Spring Boot RestTemplate
    • Spring Boot cors
    • Spring Boot HTTPS
    • Spring Boot OAuth2
    • Spring Boot Profiles
    • Spring Boot Interview Questions
    • Spring Boot filter
    • Spring boot logging level
    • Spring Boot Cache
    • Spring Boot Advantages
    • Spring Boot Scheduler
    • Spring Boot Initializr
    • Spring Boot Maven
    • Spring Boot Admin
    • Spring Boot Tomcat
    • Spring Boot WebSocket
    • Spring Boot Executable Jar
    • Spring Boot CommandLineRunner
    • Spring Boot DataSource
    • Spring Batch Scheduler
    • Spring Batch Example
    • Spring Batch Tasklet
    • Spring Batch Admin
    • Spring Batch
    • Spring Boot Qualifier
    • Spring Boot War
    • Spring Boot Test Configuration
  • Spring
    • What is Spring Framework?
    • Spring Architecture
    • What is Spring Integration?
    • IoC Containers
    • What is AOP?
    • Spring Modules
    • Spring Batch Processing
    • Spring Batch Partitioner
    • Spring Batch Job
    • Spring AOP
    • Spring Expression Language
    • Dependency Injection in Spring
    • Spring Batch Architecture
    • Spring framework Interview Questions
  • Spring Cloud Basics
    • What is Spring Cloud
    • Spring Cloud Contract
    • Spring Cloud Components
    • Spring Cloud Version
    • Spring Cloud Data Flow
    • Spring cloud stream
    • Spring Cloud Dependencies
    • Spring cloud microservices
    • spring cloud gateway
    • Spring Cloud Config
    • Spring Cloud Kubernetes
    • Spring Cloud Sleuth

Related Courses

Spring Boot Certification Course

Spring Framework Course Training

All in One Data Science Course

spring boot jdbc

spring boot jdbc

Introduction to spring boot jdbc

Spring boot jdbc provides libraries and starter to connect applications with JDBC while using JDBC database-related beans like JDBC template, named parameter JDBC template, and data source are automatically configures and creating it at the time of starting a project. To use the automatically configured classes, we can Autowired them in an application.properties file, we are configuring the connection pooling and data source. If we have not chosen any option for our project, then JDBC is choosing tomcat pooling as default. JDBC is used to connect applications using JDBC.

What is spring boot JDBC?

  • At the time of using JDBC, we have required only spring boot starter jdbc dependency.
  • Basically, JDBC is divided into four different packages are as follows.
  • Object
  • Data source
  • Core
  • Support
  • An object-oriented manner does JDBC database access. It will allow us to run the query and return the result by using an object. An object is also used to map the columns between query results and properties of objects.
  • JDBC data source is used in utility classes for accessing the data source. JDBC has many data source implementations to test the JDBC code.
  • The JDBC core package contains the JDBC core functionality. Many of the classes is important in this package which was contains the jdbc template, simple JDBC call, simple JDBC insert, and named parameter jdbc template.
  • The JDBC support package contains the support classes which was supporting for classes under object and core classes. An example of this package is providing the functionality of SQLException translation.
  • Using JDBC, we can implement database CRUD operations for the simple aggregations. JDBC is supporting for annotation name as query. It will also support events and MyBatis queries.
  • We can configure the java-based repository by using enable JDBC repositories annotation in JDBC.
  • JDBC is part of the spring boot framework, which was used to implement the JDBC repositories.
  • JDBC is used to support the JDBC data access layer. Therefore, it makes it easier to build an application that uses the connection pooling and accessing the data from the database server.
  • JDBC is the main API using this API; we can access the functionality like create and close the database connection.
  • We can execute all SQL queries and execute the statement and call of stored procedure.
  • To support the named parameter queries, we are using the JDBC template, which JDBC provided.

Why use Spring Boot JDBC?

  • At the time of developing the application using JDBC, we require only spring boot starter JDBC (spring-boot-starter-jdbc) dependency.
  • JDBC is automatically configures the bean of data source if we have not maintain it explicitly. If we do not require a bean or want to use the bean in our project, we need to set the property of spring.datasource.initialize as false. By default, spring.datasource.initialize parameter property is true.
  • Using JDBC, we have no need to register beans because it will automatically register beans at the time of starting our project.
  • Using JDBC, any database script which was contains the extension as .sql will gets automatically executed.
  • To develop an application by using JDBC, we have required only spring-boot starter dependency.
  • Using JDBC, we have no need to add the multiple dependencies to our project.
  • If we have not registered any beans like JDBC template, platform transaction manager, and named parameter jdbc template, it will register automatically using JDBC.
  • JDBC is providing the JdbcTemplate class, which was simplifies the development of JDBC.
  • As compare to hibernate, JDBC is efficient and more easy to use to develop applications.

Spring Boot and JDBC Template

Below examples show to set up the JDBC project template are as follows.

  • Create a project template using a spring initializer and give the following name to the project metadata.

Group – com.example
Artifact name – spring-boot-jdbc
Name – spring-boot- jdbc
Description - Project of spring-boot- jdbc
Package name - com.example.spring-boot- jdbc
Packaging – Jar
Java – 11
Dependencies – spring web.

All in One Software Development Bundle(600+ Courses, 50+ projects)
Python TutorialC SharpJavaJavaScript
C Plus PlusSoftware TestingSQLKali Linux
Price
View Courses
600+ Online Courses | 50+ projects | 3000+ Hours | Verifiable Certificates | Lifetime Access
4.6 (86,818 ratings)

spring boot jdbc output 1

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

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

spring boot jdbc output 2

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

spring boot jdbc output 3

  • We need the following software to develop applications using JDBC are as follows.
  1. Maven 3.2 +
  2. JDK 1.8 or later
  3. Spring boot 2.4 or later
  4. Spring tool suite
  • To develop the application using JDBC, we need to use the spring tool suite and develop a project using maven.

Spring Boot JDBC Examples

The below example shows JDBC are as follows.

  • Create database and tables 

# create database spring_boot_jdbc;
# \c spring_boot_jdbc
# create table stud (id int, name varchar(10));

output 4

  • Edit application properties file and add the database credential 

Code –

spring.datasource.url = jdbc:postgresql://localhost:5432/spring_boot_jdbc
spring.datasource.username = postgres
spring.datasource.password = postgres
spring.jpa.hibernate.ddl-auto = create-drop

spring boot jdbc output 5

  • Create spring boot JDBC application file 

Code –

@SpringBootApplication
public class SpringBootJDBC {
public static void main /* main method of spring boot JDBC application */ (String[] args) {
SpringApplication.run (SpringBootJDBC.class, args);
}
}

output 6

  • Create a controller for the JDBC application 

Code –

@RestController
public class SBJDBCController {
@Autowired
JdbcTemplate sbjdbc;
@RequestMapping("/insert")
public String index(){
sbjdbc.execute ("insert into stud values (101, 'ABC')");
}
}

output 7

  • Run the application 

output 8

  • Check the data inserted in the database table using the application 

Code –

# select * from stud;

output 9

Conclusion

JDBC database access is done in an object-oriented manner. It will allow us to run the query and return the result by using the object. JDBC is providing libraries and starters to connect applications with JDBC. JDBC is provided JdbcTemplate class to develop the application.

Recommended Articles

This is a guide to spring boot jdbc. Here we discuss What is spring boot JDBC and what is the purpose of using it, along with the templates and examples. You may also have a look at the following articles to learn more –

  1. Spring Boot RestTemplate
  2. Spring Boot OAuth2
  3. Spring Boot Profiles
  4. Spring Boot Dependencies
Popular Course in this category
Spring Boot Training Program (2 Courses, 3 Project)
  2 Online Courses |  3 Hands-on Projects |  22+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course

Related Courses

Spring Framework Training (4 Courses, 6 Projects)4.9
All in One Data Science Bundle (360+ Courses, 50+ projects)4.8
0 Shares
Share
Tweet
Share
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Java Tutorials
  • Python Tutorials
  • All Tutorials
Certification Courses
  • All Courses
  • Software Development Course - All in One Bundle
  • Become a Python Developer
  • Java Course
  • Become a Selenium Automation Tester
  • Become an IoT Developer
  • ASP.NET Course
  • VB.NET Course
  • PHP Course

ISO 10004:2018 & ISO 9001:2015 Certified

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

EDUCBA
Free Software Development Course

C# Programming, Conditional Constructs, Loops, Arrays, OOPS Concept

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

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

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

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

By signing up, you agree to our Terms of Use and Privacy Policy.

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more