Introduction to Spring Framework JDBC - Java-Springs

The value-add provided by the Spring Framework JDBC abstraction is perhaps best shown by the sequence of actions outlined in the table below. The table shows what actions Spring will take care of and which actions are the responsibility of you, the application developer.

Table :- Spring JDBC - who does what?

Spring JDBC - who does what

The Spring Frame work takes care of all the low-level details that can make JDBC such a tedious API to develop with.

Choosing an approach for JDBC database access

You can choose among several approaches to form the basis for your JDBC data base access.In addition to three flavors of the JdbcTemplate, a new Simple Jdbc Insert and SimplejdbcCall approach optimizes data base metadata, and the RDBMS Object style takes a more object-oriented approach similar to that of JDO Query design. Once you start using one of these approaches, you can still mix and match to include a feature from a different approach. All approaches require a JDBC 2.0-compliant driver, and some advanced features require a JDBC 3.0 driver.

  • JdbcTemplate is the classic Spring JDBC approach and the most popular. This "lowest level" approach and all others use a JdbcTemplate under the covers, and all are updated with Java 5 support such as generics and varargs.
  • NamedParameterJdbcTemplate wraps a JdbcTemplate to provide named parameters instead of the traditional JDBC "?" placeholders. This approach provides better documentation and ease of use when you have multiple parameters for an SQL statement.
  • SimpleJdbcTemplate combines the most frequently used operations of JdbcTemplate and Named Parameter JdbcTemplate.
  • SimpleJdbcInsert and SimpleJdbcCall optimize database metadata to limit the amount of necessary configuration. This approach simplifies coding so that you only need to provide the name of the table or procedure and provide a map of parameters matching the column names. This only works if the database provides adequate metadata. If the database doesn't provide this metadata, you will have to provide explicit configuration of the parameters.
  • RDBMS Objects including MappingSqlQuery, SqlUpdate and StoredProcedure requires you to create reusable and thread-safe objects during initialization of your data access layer. This approach is modeled after JDO Query wherein you define your query string, declare parameters, and compile the query. Once you do that, execute methods can be called multiple times with various parameter values passed in.

Package hierarchy

The Spring Framework's JDBC abstraction framework consists of four different packages, namely core, datasource, object, and support.

The org. spring frame work. jdbc. core package contains the JdbcTemplate class and its various callback interfaces, plus a variety of related classes. A sub package named org. spring frame work. jdbc. core. simple contains the Simple Jdbc Template class and the related SimpleJdbcInsert and SimpleJdbcCall classes. Another subpackage named org.springframework.jdbc.core.namedparam contains the NamedParameter Jdbc Template class and the related support classes.

The org. spring frame work.jdbc. datas ource package contains a utility class for easy Data Source access, and various simple Data Source implementations that can be used for testing and running un modified JDBC code outside of a Java EE container. A sub package named org.spring fame work.jdbc. data source.embedded provides support for creating in memory data base instances using Java database engines such as HSQL and H2.

The org. spring frame work. jdbc. object package contains classes that represent RDBMS queries, updates, and stored procedures as thread safe, reusable objects. This approach is modeled by JDO, although of course objects returned by queries are “disconnected” from the database.This higher level of JDBC abstraction depends on the lower-level abstraction in the org. spring frame work.jdbc.core package.

The org.spring fram work.jdbc. support package provides SQL Exception translation functionality and some utility classes. Exceptions thrown during JDBC processing are translated to exceptions defined in the org. spring frame work.dao package.This means that code using the Spring JDBC abstraction layer does not need to implement JDBC or RDBMS-specific error handling. All translated exceptions are unchecked, which gives you the option of catching the exceptions from which you can recover while allowing other exceptions to be propagated to the caller.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java-Springs Topics