Spring Book – Chapter 8 – Data Access

NamedParameterJdbcTemplate

The NamedParameterJdbcTemplate class adds support for programming JDBC statements using named parameters, as opposed to programming JDBC statements using only classic placeholder (‘?’) arguments. This way of doing things is more flexible as you dont have to worry about the order of the paraemetrs. The NamedParameterJdbcTemplate class wraps a JdbcTemplate, and delegates to the wrapped JdbcTemplate to do much of its work

Listing 8-18. NamedParameterJdbcTemplate usage for invoking SQL using named parameter

You can also pass along named parameters and their corresponding values to a NamedParameterJdbcTemplate instance by using the Map-based style as shown in Listing 8-19.

Listing 8-19. NamedParameterJdbcTemplate usage for invoking SQL using named parameterin map style

SimpleJdbcInsert

A SimpleJdbcInsert is a multi-threaded, reusable object providing easy insert capabilities for a table. It provides meta-data processing to simplify the code needed to construct a basic insert statement. All you need to provide is the name of the table and a Map containing the column names and the column values.

The meta-data processing is based on the java.sql.DatabaseMetaData provided by the JDBC driver. As long as the JBDC driver can provide the names of the columns for a specified table than we can rely on this auto-detection feature. If that is not the case then the column names must be specified explicitly. This was of explicitly defining columns is used in the Listing 8-20.

The actual insert is being handled using Spring’s JdbcTemplate by the way of delegation by SimpleJdbcInsert class.

Listing 8-20. Usage of SimpleJdbcInsert class to create a Customerby auto-generating customer id

In Listing 8-20, The customer id is auto-generated and then set to the customer and then the customer object is returned back. If you would like to supply the customer id yourself, there is a slight change in the way the SimpleJdbcInsert object is created, which is shown in Listing 8-21 below.

Listing 8-21. Usage of SimpleJdbcInsert class to create a Customer without auto-generating customer id

Main features of SimpleJdbcInsert can be summarized as below:

  • Simplifies database insert operation
  • Uses table meta-data to automatically provide column information to this class
  • It also has explicit way by which to provide column details
  • Supports auto generated keys in a database independent manner
  • Supports batch operation as well with ease

java.sql.DatabaseMetaData is the interface implemented by database driver vendors to let users know the capabilities of a Database Management System (DBMS). Different relational DBMS often support different features, implement features in different ways, and use different data types. The driver vendor will implement various methods which provide adequate information for the SimpleJdbcInsert class to auto-detect the columns.

Page Visitors: 10629

The following two tabs change content below.
Tomcy John

Tomcy John

Blogger & Author at javacodebook
He is an Enterprise Java Specialist holding a degree in Engineering (B-Tech) with over 10 years of experience in several industries. He's currently working as Principal Architect at Emirates Group IT since 2005. Prior to this he has worked with Oracle Corporation and Ernst & Young. His main specialization is on various web technologies and acts as chief mentor and Architect to facilitate incorporating Spring as Corporate Standard in the organization.
Tomcy John

Latest posts by Tomcy John (see all)

2 thoughts on “Spring Book – Chapter 8 – Data Access

  1. Hi my friend! I wish to say that this article is awesome, nice written and come with approximately all important infos. Iˇ¦d like to peer more posts like this .

Leave a Reply

Your email address will not be published. Required fields are marked *