Free tutorials for Java, Eclipse and Web programming



The Spring Jdbc Template for database access - Tutorial

Lars Vogel

Version 0.1

16.06.2008

Revision History
Revision 0.116.06.2008Lars Vogel
Created article

Abstract

This article describes the usage of the Spring JDBC Template in a standalone application.


Table of Contents

1. Spring JDBC
2. Spring Installation
3. Using Spring JDBC template
3.1. Create Database
3.2. Create Java Project and domain model
3.3. Create the Data Access Object (DAO)Interface
3.4. Create the Dao with the JDBC Template
3.5. Main program (for testing)
4. Thank you
5. Questions and Discussion
6. Links and Literature
6.1. Source Code
6.2. Spring Links

1. Spring JDBC

Spring provides a simplification in handling database access with the Spring JDBC Template.

The Spring JDBC Template has the following advantages compared with standard JDBC.

  • The Spring JDBC template allows to clean-up the resources automatically, e.g. release the database connections.

  • The Spring JDBC template converts the standard JDBC SQLExceptions into RuntimeExceptions. This allows the programmer to react more flexible to the errors. The Spring JDBC template converts also the vendor specific error messages into better understandable error messages.

The Spring JDBC template offers several ways to query the database. queryForList() returns a list of HashMaps. The name of the column is the key in the hashmap for the values in the table.

More convenient is the usage of ResultSetExtractor or RowMapper which allows to translates the SQL result direct into an object (ResultSetExtractor) or a list of objects (RowMapper). Both these methods will be demonstrated in the coding.