Spring JDBC Development – A Practical Approach

Spring JDBC Development – A Practical Approach

The general architecture of an application includes a few layers, as presented into the picture below:
The general architecture of an application includes a few layers, as presented into the picture below:

general_architecture_of_an_application.png


As an overview of this structure, it includes:

  • The presentation layer: web-browsers, mobile applications, standalone applications – it shows the data and interacts with the user
  • The business service layer, it implements the business logic. Business objects are used by the business service. They have to be synchronized with the persistence layer.
  • The data access layer ensures synchronization between business objects and the persistence layer. For each domain entity, we should create a separate DAO (Data Access Object) class.

We would like to show how to write DAOs with the help of Spring.

The role of the JDBC is to allow the uniform interaction from the application to the database. JDBC is using the drivers, one for each database. The drivers translate the commands of the JDBC API to the commands of the database system.

Spring_JDBC.jpg


One may wonder why to use Spring JDBC, as long as we already have ORM? There are a reasons for this, to enumerate a few of them:

  • Spring JDBC provides flexibility; it is using all RDBMS possibilities.
  • Spring JDBC is transparent – everything is under the control, while ORM is creating the SQL commands by itself.
  • Spring JDBC provides higher performance.
  • Last, but not least, as long you still have control, there will be no magic behind your work with the database.

In this case, why plain JDBC, as you may know it, is not enough? There are also a few reasons:

  • Plain JDBC still requires manual exception handling.
  • Plain JDBC involves manual transaction management.
  • Plain JDBC does not provide mapping of data to the objects.
  • And there is a big amount of service code.


The DAO design pattern involves the matching between a business object and a database table. We may presume that we start with the following interface and with the following database table:

DAO_design_pattern.JPG


Our goal is to persist a Book object from memory to the BOOK table from the database. In order to work with plain JDBC in such a case, the code may look like this:

plain_JDBC.JPG


Having a look at the work we need to do using plain JDBC, we can point out the following sequence:

  • Define connection parameters
  • Open the connection
  • Specify the statement
  • Prepare and execute the statement
  • Iterate through the results
  • Do the work for each iteration
  • Process any exception
  • Handle transactions
  • Close the connection
In our next article we will look at how we can do this using Spring JDBC.

Interested in improving your Spring programming skills? Check out our trainings: Spring Framework for Application Development, Spring Core and Spring Databases.

Catalin Tudose
Java and Web Technologies Expert
Залишилися запитання?
Зв'яжітьсяз нами