0% found this document useful (0 votes)
25 views3 pages

Ajp Unit 5

The document discusses using JDBC Template and Spring JDBC for data access operations. It describes the different types of JDBC drivers and advantages of JDBC API. It also explains using Spring JDBC Template and the different approaches available for JDBC database access in Spring framework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views3 pages

Ajp Unit 5

The document discusses using JDBC Template and Spring JDBC for data access operations. It describes the different types of JDBC drivers and advantages of JDBC API. It also explains using Spring JDBC Template and the different approaches available for JDBC database access in Spring framework.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

ADVANCED JAVA PROGRAMMING

NOTES BY: DR. PANKAJ MALIK


UNIT – 5 - JDBC AND SPRING BOOT

Data Access operations with JDBC Template and Spring


JDBC (Java Database Connectivity) is an application programming interface (API) that
defines how a client may access a database. It is a data access technology used for Java
database connectivity. It provides methods to query and update data in a database and is
oriented toward relational databases. JDBC offers a natural Java interface for working
with SQL. JDBC is needed to provide a “Pure Java” solution for application development.
JDBC API uses JDBC drivers to connect with the database.

The org.springframework.jdbc.core.JdbcTemplate class is the central class in the JDBC core


package. It simplifies the use of JDBC and helps to avoid common errors. It executes core JDBC
workflow, leaving the application code to provide SQL and extract results. This class executes
SQL queries or updates, initiating iteration over ResultSets and catching JDBC exceptions and
translating them to the generic, more informative exception hierarchy defined in
the org.springframework.dao package.

Types of JDBC Drivers


There are 4 types of JDBC Drivers.

1. JDBC-ODBC Bridge Driver


2. Native API Driver (partially Java driver)
3. Network Protocol Driver (fully Java driver)
4. Thin Driver (fully java driver)

Advantages of JDBC API

 Automatically creates the XML format of data from the database.


 It supports query and stored procedures.
 Almost any database for which ODBC driver is installed can be accessed.
Data Access using Spring JDBC Template
There are a number of options for selecting an approach to form the basis for
your JDBC database access. Spring framework provides the following approaches
for JDBC database access:

1. JdbcTemplate
2. NamedParameterJdbcTemplate
3. SimpleJdbcTemplate
4. SimpleJdbcInsert and SimpleJdbcCall

You can choose among several approaches to form the basis for your JDBC database access. In
addition to three flavors of the JdbcTemplate, a new SimpleJdbcInsert and SimplejdbcCall
approach optimizes database metadata, and the RDBMS Object style takes a more object-
oriented approach similar to that of JDO Query design. Once you start using one of these
approaches, you can still mix and match to include a feature from a different approach. All
approaches require a JDBC 2.0-compliant driver, and some advanced features require a JDBC
3.0 driver.

 JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest
level" approach and all others use a JdbcTemplate under the covers, and all are updated
with Java 5 support such as generics and varargs.
 NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters
instead of the traditional JDBC "?" placeholders. This approach provides better
documentation and ease of use when you have multiple parameters for an SQL statement.
 SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate
and NamedParameterJdbcTemplate.
 SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount
of necessary configuration. This approach simplifies coding so that you only need to
provide the name of the table or procedure and provide a map of parameters matching the
column names. This only works if the database provides adequate metadata. If the
database doesn't provide this metadata, you will have to provide explicit configuration of
the parameters.
 RDBMS Objects including MappingSqlQuery, SqlUpdate and
StoredProcedure requires you to create reusable and thread-safe objects during
initialization of your data access layer. This approach is modeled after JDO Query
wherein you define your query string, declare parameters, and compile the query. Once
you do that, execute methods can be called multiple times with various parameter values
passed in.
1. JDBC Template
JdbcTemplate is a central class in the JDBC core package that simplifies the use
of JDBC and helps to avoid common errors. It internally uses JDBC API and eliminates a lot
of problems with JDBC API. It executes SQL queries or updates, initiating iteration over
ResultSets and catching JDBC exceptions and translating them to the generic. It executes
core JDBC workflow, leaving application code to provide SQL and extract results. It handles
the exception and provides the informative exception messages with the help of exception
classes defined in the org.springframework.dao package.

Spring Boot and Database


To work with databases in Spring Boot, we will be using the Spring Data JPA module. JPA
stands for Java Persistence API, which is a specification for managing relational data in Java
applications. Spring Data JPA provides a set of abstractions on top of JPA that makes it easier
to work with databases in Spring Boot

Step 1. Define the database configuration in the application.properties file


Spring Boot allows you to define the database configuration in a properties file. This file is
usually named application.properties and is located in the src/main/resources folder. You can
define the database URL, username, password, and driver class in this file. Below is an
example of how you can define the database configuration in the application.properties file:
Step 2. Create a model class
Next, you need to create a model class that represents the data you want to store in the
database. This class should have fields corresponding to the database table’s columns. Let’s
say we want to store information about books in our database.
Step 3. Create a repository interface
After creating the model class, you need to create a repository interface. This interface should
extend the JpaRepository interface provided by Spring Data JPA. This interface defines a set of
methods for performing CRUD (Create, Read, Update, Delete) operations on the database. We
need to create a repository interface for the Book model.
Step 4. Use the repository in the service layer
Now that you have a repository interface, you can use it in the service layer. The service layer
is responsible for implementing business logic and using the repository to interact with the
database. Let’s create a BookService class that will use the BookRepository to perform CRUD
operations on the database.
Step 5. Use the service layer in the controller
Finally, you can use the service layer in the controller to handle HTTP requests and responses.
The controller is responsible for handling incoming HTTP requests, calling the appropriate
service method, and returning a response to the client. Let’s create a BookController class that
will handle HTTP requests and use the BookService to perform CRUD operations on the
database.

You might also like