- Spring JDBC - Discussion
- Spring JDBC - Useful Resources
- Spring JDBC - Quick Guide
- Spring JDBC - StoredProcedure
- Spring JDBC - SqlUpdate
- Spring JDBC - SqlQuery
- Spring JDBC - SimpleJdbcCall
- Spring JDBC - SimpleJdbcInsert
- NamedParameterJdbcTemplate
- Spring JDBC - RowMapper
- Spring JDBC - ResultSetExtractor
- PreparedStatementSetter
- Spring JDBC - JdbcTemplate
- Multiple Batches Operation
- Objects Batch Operation
- Spring JDBC - Batch Operation
- Spring JDBC - Handling CLOB
- Spring JDBC - Handling BLOB
- Spring JDBC - Calling Stored Function
- Calling Stored Procedure
- Spring JDBC - Delete Query
- Spring JDBC - Update Query
- Spring JDBC - Read Query
- Spring JDBC - Create Query
- Spring JDBC - First Application
- Spring JDBC - Configure Data Source
- Spring JDBC - Environment Setup
- Spring JDBC - Overview
- Spring JDBC - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Spring JDBC - Overview
While working with database using plain old JDBC, it becomes cumbersome to write unnecessary code to handle exceptions, opening and closing database connections, etc. However, Spring JDBC Framework takes care of all the low-level details starting from opening the connection, preparing and executing the SQL statement, processing exceptions, handpng transactions, and finally closing the connection.
What you have do is just define connection parameters and specify the SQL statement to be executed and do the required work for each iteration while fetching data from the database.
Spring JDBC provides several approaches and correspondingly different classes to interface with the database. In this tutorial, we will take classic and the most popular approach which makes use of JDBC Template class of the framework. This is the central framework class that manages all the database communication and exception handpng.
JDBC Template Class
JDBC Template class executes SQL queries, updates statements and stored procedure calls, performs 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 threadsafe 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 when using the JDBC Template class is to configure a DataSource in your Spring configuration file, and then dependency-inject that shared DataSource bean into your DAO classes. The JDBC Template is created 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 functionapty through an interface by which the rest of the apppcation will access them.
The Data Access Object (DAO) support in Spring makes it easy to work with data access technologies such as JDBC, Hibernate, JPA, or JDO in a consistent way.
Advertisements