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
- Specify the statement.
- Do the work for each iteration.
- Define parameters for connection.
- Declaration of parameters and providing value for parameter.
The responsibilities of Spring are
- Define connection parameters
- Open the connection
- Prepare and execute the statement
- Set up the loop to iterate through the results (if any)
- Process any exception
- Handle transactions
- Close the connection
Spring Framework JDBC Packages
The spring framework JDBC consists of four types of packages.
- Core package
- DataSource package
- Object package
- Support package
The org.springframework.jdbc.core package includes
- The JdbcTemplate class
- Its various callback
- 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.
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.
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.
- Jdbc template
- Executing statements
- Running Queries
- Updating the database
In the JDBC core package the JdbcTemplate class is the central class. It handles the creation and release of resources. This helps to
- Avoid Common errors: – Eg: – Forgetting to always close the connection.
- Executes the core JDBC workflow E.g.:- Statement creation and execution, leaving application to provide SQL and extract results.
- 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.
int rowCount = this.jdbcTemplate.queryForInt("select count(0) from t_accrual");