Using Spring JDBC To Interact With A Database


For a new project I'm working on I'm using Spring JDBC ( 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 /spring/ (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 /spring/

You also need the Derby database used by the example application.  You can download that database here /spring/  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 file.

The example application demonstrates most of the Spring JDBC techniques explained in the Spring 2.5 documentation (  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 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.


Chapter 11. Data access using JDBC,

Spring Framework API 2.5,

Spring in Action, Second Edition (Manning 2007), Chapter 5,

Pro Spring 2.5 (Apress 2008), Chapter 9,

Spring JDBC Examples, /spring/

Spring JDBC Examples Derby Database, /spring/

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 Contact Blog Owner