Chapter 3, DAO and JDBC in Spring

Q1. Explain Spring JDBC packages.

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

Spring JDBC packages

Description

org.springframework.jdbc.core

In the Spring Framework, this package contains the foundations of JDBC classes, which includes 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 JEE container.

org.springframework.jdbc.object

In the Spring Framework, this package contains classes that helps 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. Spring recognizes the error code used by database using this class, and map error code to higher-level exception.

org.springframework.jdbc.config

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

Q2. What is JdbcTemplate?

The JdbcTemplate class instances are thread-safe once configured. A single JdbcTemplate can be configured and injected in multiple DAOs. We can use JdbcTemplate to execute different types of SQL statements. Data Manipulation Language (DML) is used to insert, retrieve, update, and delete data in database. The SELECT, INSERT, or UPDATE statements are examples of DML. Data Definition Language (DDL) is used to either create or modify the structure of database objects in database. The CREATE, ALTER, and DROP statements are examples of DDL.

Q3. Explain the JDBC operation in Spring.

The single executable unit for performing multiple operations is known as a batch. The batch update operation allows submitting multiple of SQL queries DataSource for processing at once. Submitting multiple SQL queries together, instead of individually improves the performance. The JdbcTemplate includes a support for executing the batch of statements through a JDBC Statement and PreparedStatement. The JdbcTemplate includes two overloaded batchUpdate() methods in support of this feature:

  • One for executing a batch of SQL statements using JDBC Statement like:
    public int[] batchUpdate(String[] sql) throws DataAccessException
  • The other for executing the SQL Statement for multiple times with different parameters using PreparedStatement such as:
    public int[] batchUpdate(String sql, BatchPreparedStatementSetter bPSS) throws DataAccessException
..................Content has been hidden....................

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