Spring JDBC packages

In the previous section, we have seen the shortcomings of using the JDBC API as a low-level data access API for implementing the DAOs. These shortcomings are as follows:

  • Code duplication: As we know, writing the boilerplate code over and over again in code duplication violates the Don't repeat yourself (DRY) principle. This has some side effects in terms of the project costs, efforts, and timelines.
  • Resource leakage: The DAO methods must hand over the control of the obtained database resources, such as connection, statements, or result sets after calling the close() method. This is a risky plan because a novice programmer might very easily skip some of the code fragments. As a result, the resources would run out and bring the system to a stop.
  • Error handling: When using JDBC directly we need to handle SQLException, since the JDBC drivers report all the errors suitable by raising SQLException. It is not possible to recover these exceptions. Moreover, the message and the error code obtained from the SQLException object are database vendor-specific, so it is difficult to write a portable DAO error messaging code.

To solve the aforementioned problems, we need to identify the parts of the code that are fixed and then encapsulate them into some reusable objects. The Spring Framework provides a solution for these problems by giving a thin, robust, and highly extensible JDBC abstraction framework.

The JDBC abstraction framework provided under the Spring Framework is considered to be a value-added service that takes care of all the low-level details, such as retrieving connection, preparing the statement object, executing the query, and releasing the database resources. While using it for data access, the application developer needs to specify the SQL statement for executing and retrieving the result.

To handle the different aspects of JDBC, Spring JDBC is divided into packages, as shown in the following table:

Spring JDBC package

Description

org.springframework.jdbc.core

In the Spring Framework, this package contains the foundation of the JDBC classes, which includes the core JDBC class and JdbcTemplate. It simplifies the database operation using JDBC.

org.springframework.jdbc.datasource

This package contains DataSource implementations and helper classes, which can be used to run the JDBC code outside the JEE container.

org.springframework.jdbc.object

In the Spring Framework, this package contains the classes that help in converting the data returned from the database into plain Java objects.

org.springframework.jdbc.support

SQLExceptionTranslator is the most important class in this package of the Spring Framework. The Spring Framework recognizes the error code used by the database. This is done by using this class and mapping the error code to a higher level of exception.

org.springframework.jdbc.config

This package contains the classes that support JDBC configuration within ApplicationContext of the Spring Framework.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.139.83.7