Using Spring JDBC To Interact With A Database

Introduction

For a new project I'm working on I'm using Spring JDBC (http://static.springsource.org/spring/docs/2.5.x/reference/jdbc.html) to interact with the database. 

Spring JDBC provide numerous benefits over using traditional JDBC.  Once you learn the methodology of using Spring JDBC, development time is greatly reduced.  Spring JDBC has numerous methods that make it simple to store, retrieve, and update your Java objects in the database. 

Additionally Spring JDBC manages your database connection objects (you just need to provide a data source) and provides a greatly expanded runtime exception library built over top of Java's checked SQLException.  Using Spring JDBC you just need to write the code that "moves data to and from the database." [Spring in Action, page 173]  All the other "boiler-plate" code you used to write using traditional Java JDBC is handled by Spring JDBC.

Spring JDBC Example Application

To help myself and others learn Spring JDBC I put together an example Java application that uses it.  You can download this example at http://www.brucephillips.name/spring/springjdbcexamples.zip (MyEclipse Java Maven Project). You should be able to import the example application directly into MyEclipse (File - Import - Existing Project Into Workspace - Select Archive File [Browse to the downloaded zip file]) or import the unzipped download into plain Eclipse as a Maven project. You can also download a plain Eclipse 3.5 (with Maven 2 plugin) archived Java project at http://www.brucephillips.name/spring/springjdbcexamples_eclipse.zip.

You also need the Derby database used by the example application.  You can download that database here http://www.brucephillips.name/spring/springjdbcexamplesDB.zip.  Unzip the download file into a folder named c:/derby.

To run the example application execute the SpringJDBCExamplesApp class.  The database properties are read from the db.properties file.

The example application demonstrates most of the Spring JDBC techniques explained in the Spring 2.5 documentation (http://static.springsource.org/spring/docs/2.5.x/reference/jdbc.html).  The JavaDoc for class PersonDao explains which methods are examples for what parts of the Spring JDBC reference.

One thing to note about the Spring JDBC online documentation at http://static.springsource.org/spring/docs/2.5.x/reference/jdbc.html is that the first set of examples use the methods of the JDBCTemplate class. The JDBCTemplate class is what you should use if you're still using Java 1.4.

If you are using Java 1.5 (Java 5) you should use the newer SimpleJDBCTemplate class. The methods of the SimpleJDBCTemplate class take advantage of the new Java 1.5 features such as variable method arguments, auto boxing/unboxing, and covariant return types. The latter examples in the documentation (and in my example application) use the SimpleJDBCTemplate class.

References

Chapter 11. Data access using JDBC, http://static.springsource.org/spring/docs/2.5.x/reference/jdbc.html

Spring Framework API 2.5, http://static.springsource.org/spring/docs/2.5.x/api/index.html

Spring in Action, Second Edition (Manning 2007), Chapter 5, http://www.manning.com/walls3/

Pro Spring 2.5 (Apress 2008), Chapter 9, http://www.apress.com/book/view/9781590599211

Spring JDBC Examples, http://www.brucephillips.name/spring/springjdbcexamples.zip

Spring JDBC Examples Derby Database, http://www.brucephillips.name/spring/springjdbcexamplesDB.zip

Comments (Comment Moderation is enabled. Your comment will not appear until approved.)
Awesome! Thanks for the examples.
# Posted By Dan | 9/25/09 9:44 PM
BlogCFC was created by Raymond Camden. This blog is running version 5.9.1.002. Contact Blog Owner