Spring JDBC - Overview

We know that, to work with database, we are using plain old JDBC, So it is the developer responsibility to write extra code to handle exceptions, opening and closing database connections, etc. 

Later, Spring JDBC Framework takes care of all the low-level details starting from opening the connection, preparing and executing the SQL statement,  processing exceptions, handling transactions, and finally closing the connection.

So developer no need to bother about this. What we have do is just define connection parameters and specify the SQL statement to be executed and do the required work. 

Spring JDBC provides several approaches and correspondingly different classes and interfaces to work with the database. In this tutorial, we will learn the most popular approach i.e. JDBC Template class of the framework. 

This is the central framework class that manages all the database communication and exception handling.

JDBC Template Class :-

A JDBC Template class  can be used to 

  • execute SQL queries, 
  • update statements and stored procedure calls, 
  • perform iteration over ResultSets and extraction of returned parameter values.

It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.

Instances of the JDBC Template class are thread safe once configured. So, you can configure a single instance of a JDBC Template and then safely inject this shared reference into multiple DAOs.

A common practice to use the JDBC Template class is to :- 

  1. Configure a DataSource in your Spring configuration file, 
  2. Inject that shared DataSource bean into your DAO classes. 
  3. Create JDBC Template  in the setter for the DataSource.

Data Access Object (DAO) :- 

DAO stands for Data Access Object which is commonly used for database interaction. DAOs exist to provide a means to read and write data to the database and they should expose this functionality through an interface by which the rest of the application will access them.

The Data Access Object (DAO) provided by Spring, makes it easy to work with data access technologies such as JDBC, Hibernate, JPA, or JDO in a consistent way.