Spring JDBC

The spring JDBC is a type of data access technology. This technology is used for accessing data from database. The spring DAO (Data access object) make it easy for using this technology. There are lot of examples listed here. By using this technology we can reduce a lot of overhead and also we don’t need to worry about Exception handling that is related to each technology. JDBC provide some actions and responsibilities. Some responsibilities are taken by developers and others are taken by Spring itself.

The developer’s responsibilities are

  1. Specify the statement.
  2. Do the work for each iteration.
  3. Define parameters for connection.
  4. Declaration of parameters and providing value for parameter.

The responsibilities of Spring are

  1. Define connection parameters
  2. Open the connection
  3. Prepare and execute the statement
  4. Set up the loop to iterate through the results (if any)
  5. Process any exception
  6. Handle transactions
  7. Close the connection

Spring Framework JDBC Packages

The spring framework JDBC consists of four types of packages.

  1. Core package
  2. DataSource package
  3. Object package
  4. Support package

The org.springframework.jdbc.core package includes

  1. The JdbcTemplate class
  2. Its various callback
  3. Different related classes

The org.springframework.jdbc.datasource package includes,

a)    A utility class

Utility classes are used for easy DataSource access. This class provides static methods to obtain connections from JNDI and to close connections if necessary. It permits the thread bound connections. E.g.:- Use of DataSourceTransactionManager.

b)    Different simple DataSource

Simple DataSource implements help for testing and running unmodified JDBC code outside of a J2EE container.

Object package

The org.springframework.jdbc.object package includes,

Several classes that represent RDBMS queries, updates, and stored procedures as thread safe, reusable objects. This approach is modeled by JDO, although objects returned by queries are “disconnected” from the database.

Support package

The org.springframework.jdbc.support package is helps to find SQLException translation functionality and some utility classes.

We are using different approaches for database access.

  1. Jdbc template
  2. NamedParameterJdbcTemplate
  3. SimpleJdbcTemplate
  4. DataSource
  5. SOLExceptionTranslator
  6. Executing statements
  7. Running Queries
  8. Updating the database

Jdbc Template

In the JDBC core package the JdbcTemplate class is the central class. It handles the creation and release of resources. This helps to

  1. Avoid Common errors: – Eg: – Forgetting to always close the connection.
  2. Executes the core JDBC workflow E.g.:- Statement creation and execution, leaving application to provide SQL and extract results.
  3. Also executes SQL queries, update statements or stored procedure calls, imitating iteration over ResultSets and extraction of returned parameter values.

The JdbcTemplate can be applied within a DAO implementation via direct instantiation with a DataSource reference, or be configured in a spring IOC container and given to DAOs as a bean reference.

Example: –

 int rowCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual");