Spring Book – Chapter 8 – Data Access

JDBC Namespace

This is one of the new features introduced in Spring 3.0. To use the jdbc namespace you’ll need to include the line as shown in Listing 8-31 in your application configuration namespaces.

Listing 8-31. jdbc namespace configuration in your application configuration XML file

You should also include the schema location as shown in Listing 8-32.

Listing 8-32. jdbc namespace schema configuration in your application configuration XML file

The Jdbc namespace has two important elements. They are:

  • initialize-database – which allows to initialize database with provided scripts
  • embedded-database – which allows to initialize an embedded database

initialize-database

Can use the XML tags to initialize the data-source with the scripts provided. Listing 8-33 shows initializing database using the scripts.

Listing 8-33. XML configuration to initialize datasource using jdbc namespace

The various properties which can be used in the jdbc namespace in Listing 8-33 are:

    • data-source – javax.sql.DataSource bean reference. Will be used to connect and execute the provided scripts.
    • enabled – should it be enabled or not
    • ignore-failures – specify that whether it should be ignored if statements end with errors.

This tag can have zero or more script tags and the property which this tag has is the location property.

embedded-database

Listing 8-34. XML configuration for embedded database using jdbc namespace

The main attributes in this are:

  • id – becomes the bean name
  • type – embedded database type. Supported databases are namely H2, Derby and HSQL. HSQL is the default database if it is not specified.

Spring JDBC Callback Interfaces

Even though most of the low level details related to JDBC API access such as Connections, Statements, ResultSets etc are hidden or abstracted from the developers, there may still be situations where the client code want to intercept the calling of the underlying APIs. For such situations Spring provides hook in the form of callbacks.

PreparedStatementCreator

The interface PreparedStatementCreator allows the client code to create Prepared Statement instances which represent the pre-compiled version of the SQL query passed. Lets assume that we would like to select the customer object based on customer primary key id. Since for every select operation, the customer id will be changing, we will pre-compile this query using PreparedStatement as shown in Listing 8-35.

Listing 8-35. Usage of PreparedStatementCreator

RowCallbackHandler

An interface used by JdbcTemplate for processing rows of a ResultSet on a per-row basis. Implementations of this interface perform the actual work of processing each row but don’t need to worry about exception handling. SQLExceptions will be caught and handled by the calling JdbcTemplate.

Listing 8-36. Usage of RowCallbackHandler

Page Visitors: 10627

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 *