Friday, December 24, 2010

Spring 3 MVC - JDBC Integration Tutorial

In this tutorial we will integrate database access using JDBC with a simple Spring 3 MVC application. We will manage a list of persons and provide a simple CRUD system for viewing, adding, editing, and deleting. We will use HyperSQL as our database, though the application is flexible enough to utilize a different database. The goal of this tutorial is to show how to integrate JDBC with Spring using annotations. I assume my readers have a knowledge of Spring MVC and relational databases. It would be beneficial to my readers to compare this tutorial with my other tutorial Spring 3 MVC - Hibernate 3: Using Annotations Integration Tutorial.

What is JDBC?
The Java Database Connectivity (JDBC) API is the industry standard for database-independent connectivity between the Java programming language and a wide range of databases – SQL databases and other tabular data sources, such as spreadsheets or flat files. The JDBC API provides a call-level API for SQL-based database access.
What is HyperSQL?
HSQLDB (HyperSQL DataBase) is the leading SQL relational database engine written in Java. It has a JDBC driver and supports nearly full ANSI-92 SQL (BNF format) plus many SQL:2008 enhancements. It offers a small, fast multithreaded and transactional database engine which offers in-memory and disk-based tables and supports embedded and server modes. Additionally, it includes tools such as a command line SQL tool and GUI query tools.


Here's the folder structure of our application:

Let's begin by defining our Person object.

This is a simple POJO with four fields:
Since we will manipulate a list of persons, let's declare a service that manipulates a list of Persons.


We've declared a simple CRUD system with the following methods:
In each method we prepared a sql String (for example):
String sql = "delete from person where id = ?";
If you have worked with a relational database before, that statement should be familiar already to you.

We also assigned parameters in each statement if required (for example):
Map parameters = new HashMap();
  parameters.put("firstName", firstName);
  parameters.put("lastName", lastName);
  parameters.put("money", money);
Here we're using Named Parameters so that we easily interchange the order of the parameters.

Notice in each method the actual database action is delegated to an instance of SimpleJdbcTemplate

What is a SimpleJdbcTemplate?
The SimpleJdbcTemplate class wraps the classic JdbcTemplate and leverages Java 5 language features such as varargs and autoboxing.

What is a JdbcTemplate?
The JdbcTemplate class is the central class in the JDBC core package. It handles the creation and release of resources, which helps you avoid common errors such as forgetting to close the connection. It performs the basic tasks of the core JDBC workflow such as statement creation and execution, leaving application code to provide SQL and extract results. The JdbcTemplate class executes SQL queries, update statements and stored procedure calls, performs iteration over ResultSets and extraction of returned parameter values. It also catches JDBC exceptions and translates them to the generic, more informative, exception hierarchy defined in the org.springframework.dao package.

We're done with the Person object and the Service layer. Let's move to the Spring controller.


This controller declares four mappings:
Each mapping delegates the call to the PersonService. When the PersonService is done processing, the controller then forwards the request to a JSP page that displays a confirmation message.

Added Page


Edited Page


Deleted Page


Main Page

To finish our Spring MVC application, we need to declare a couple of required XML configurations.

To enable Spring MVC we need to add it in the web.xml


Take note of the URL pattern. When accessing any pages in our MVC application, the host name must be appended with

In the web.xml we declared a servlet-name spring. By convention, we must declare a spring-servlet.xml as well.


By convention, we must declare an applicationContext.xml as well.


If you're following my previous tutorials, at this point, our application should now be finished. But we're not done yet. Notice in the applicationContext.xml, we declared the following import:


We basically encapsulated all JDBC and Spring related configurations in this one XML file. Here's what happening within the config:

1. Enable transaction support through Spring annotations:

2. Declare a datasource:

Our datasource uses C3P0 for pooling to allow efficient access to our database. Why do we need to wrap our datasource with a connection pool?
JDBC connections are often managed via a connection pool rather than obtained directly from the driver. Examples of connection pools include BoneCP, C3P0 and DBCP.


What is Pooling?
In software engineering, a connection pool is a cache of database connections maintained so that the connections can be reused when future requests to the database are required. Connection pools are used to enhance the performance of executing commands on a database. Opening and maintaining a database connection for each user, especially requests made to a dynamic database-driven website application, is costly and wastes resources. In connection pooling, after a connection is created, it is placed in the pool and it is used over again so that a new connection does not have to be established.


There are different providers of pooling. C3P0 is one of the good ones.
For more info on configuring C3P0, you can check this reference from JBoss: HowTo configure the C3P0 connection pool. For a list of other pooling providers, see Open Source Database Connection Pools

The database-specific configuration are contained within a properties file.

As an alternative, we can enter these properties directly within the jdbc-context.xml

This is exactly similar to the following:

The benefit of using a separate properties file is we encapsulate all database-specific configs within a separate file. The jdbc-context.xml purpose is to encapsulate JDBC-related config not database properties.

Our application is now finished. We've managed to setup a simple Spring 3 MVC application with JDBC support. We've managed to build a CRUD system using JDBC. We've also leveraged Spring's MVC programming model via annotation.

To access the main page, enter the following URL:

To add a new user, enter the following URL:

To delete a user, enter the following URL:

To edit a user, enter the following URL:

Just change the URL parameters to match the id that you're editing. If the id doesn't exist or the format is incorrect, expect an error to be thrown.

In order to make the application run, you need to run an instance of HSQLDB. If you're using Eclipse, all you need to do is import the whole project. Then find the hsqldb-2.0.0.jar on under the Libraries then do the following:
1. Right-click on the hsqldb-2.0.0.jar.
2. Select Run As.
3. Choose Server - org.hsqldb.server. This will run an instance of HSQLDB.

If you need a GUI, you can run the built-in Swing interface:
Here's how it looks like:

If you need further help, consult the HSQLDB documentation. If you want to use a different database, like MySQL, make sure to include the Java drivers first. Then update the and hibernate.cfg.xml accordingly.

Because we're using JDBC here, we need to create the database schema manually. I have included the database sql_script within the project itself. You can import the schema from HSQLDB's Swing interface. The actual sql_script is really short:

The best way to learn further is to try the actual application.

Download the project
You can access the project site at Google's Project Hosting at

You can download the project as a Maven build. Look for the in the Download sections.

You can run the project directly using an embedded server via Maven.
For Tomcat: mvn tomcat:run
For Jetty: mvn jetty:run

If you want to learn more about Spring MVC and integration with other technologies, feel free to read my other tutorials in the Tutorials section.
StumpleUpon DiggIt! Blinklist Yahoo Furl Technorati Simpy Spurl Reddit Google I'm reading: Spring 3 MVC - JDBC Integration Tutorial ~ Twitter FaceBook

Subscribe by reader Subscribe by email Share


  1. Whenever i run jdbc call i get following information in my console, am i dong any wrong here??? but query is executing without any problem.
    fyi: i'm using oracle as my database.

    14 Jun 2011 11:50:47,636 INFO [STDOUT] 14 Jun 2011 11:50:47,636 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 1
    14 Jun 2011 11:50:47,636 INFO [STDOUT] 14 Jun 2011 11:50:47,636 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 2
    14 Jun 2011 11:50:47,636 INFO [STDOUT] 14 Jun 2011 11:50:47,636 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 3
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 4
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 5
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 6
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 7
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 8
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 9
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] incremented pending_acquires: 10
    14 Jun 2011 11:50:47,652 INFO [STDOUT] 14 Jun 2011 11:50:47,652 DEBUG [com.mchange.v2.resourcepool.BasicResourcePool] com.mchange.v2.resourcepool.BasicResourcePool@6f8860 config: [start -> 3; min -> 10; max -> 100; inc -> 5; num_acq_attempts -> 30; acq_attempt_delay -> 1000; check_idle_resources_delay -> 60000; mox_resource_age -> 0; max_idle_time -> 0; excess_max_idle_time -> 0; destroy_unreturned_resc_time -> 0; expiration_enforcement_delay -> 0; break_on_acquisition_failure -> false; debug_store_checkout_exceptions -> false]

  2. Is there any way to call oracle Stored Procedures??

  3. this is poor web site for spring tutorial

  4. For the jdbc-context.xml

    Getting the prefix "p" for attribute "p:driverClass" associated with an element type "bean" is not bound

    any ideas?

  5. I am getting exceptions about application contex while running this program

  6. can u help me out where exactly the problem the following exception occired.
    org.springframework.beans.factory.BeanDefinitionStoreException: IOException parsing XML document from ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(
    at org.springframework.web.context.ContextLoader.createWebApplicationContext(
    at org.springframework.web.context.ContextLoader.initWebApplicationContext(
    at org.springframework.web.context.ContextLoaderListener.contextInitialized(
    at org.apache.catalina.core.StandardContext.listenerStart(
    at org.apache.catalina.core.StandardContext$
    at org.apache.catalina.core.StandardContext$
    at java.util.concurrent.FutureTask$Sync.innerRun(Unknown Source)
    at Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$ Source)
    at Source)
    Caused by: Could not open ServletContext resource [/WEB-INF/applicationContext.xml]
    at org.springframework.beans.factory.xml.XmlBeanDefinitionReader.loadBeanDefinitions(
    ... 20 more

  7. @chandra, the exception states " Could not open ServletContext resource [/WEB-INF/applicationContext.xml]". Are you sure you have a web.xml in your project and it is in the correct location? It should be inside the WEB-INF folder

  8. I have kept web.xml inside WEB-INF only but it is showing same error but i think that this exception is about contextLoaderListener class so......It's not firing even i have given the url like myapplicationname/krams/main as it is i have given.can u help me out how to perform listener tag as well in it.

  9. @chandra, can you send me a copy of your project if possible? Or a simplified version of it. Send it to (krams915 at google dot com). I purposely spelled out my email to lessen spam from bots. Based on the exception, the web.xml isn't found. I can't verify that it's the ContextLoaderListener really.

  10. Thanks krams,I have sent my project to ur mail.Pls rectify and forward as early as possible.

  11. Looks good. One question though - say after I have deployed the application, I want to change the database, does Spring provide a way to do this?

    If I modify the properties, will Spring start creating new connections based on those properties?


  12. @CodeMonkey, you mean switch db on-the-fly? If that's what you meant, yes it can. See the following link:

  13. Excellent tutorial krams,

    Spring is an excellent framework, but really lacks in documentation.

    Thanks for being the light at the end of the tunnel.


  14. I am trying to use this example with sqlserver.I get the following error. If anyone would want to take a closer look at the config files or my project, I would happy to send it.

    [ERROR] [Thread-10 08:06:54] ( Connections could not be acquired from the underlying database!
    [ERROR] [Thread-10 08:07:14] ( Connections could not be acquired from the underlying database!
    [ERROR] [Thread-10 08:07:14] ( schema export unsuccessful
    java.sql.SQLException: Connections could not be acquired from the underlying database!
    at com.mchange.v2.sql.SqlUtils.toSQLException(
    at com.mchange.v2.c3p0.impl.C3P0PooledConnectionPool.checkoutPooledConnection(
    at ...

  15. @Adel, it's probable that you've declared your database name or database driver name incorrect. Also, though this is obvious, it's possible that your sql server isn't running. "Connections could not be acquired from the underlying database!" is the error

  16. This comment has been removed by the author.

  17. I successfully ran a simple java application with the same specs. Below are the specs that I changed for my example. If you can please take a quick look.

    # database properties
    #hibernate properties

    # hibernate.cfg.xml

    property name="hibernate.dialect">org.hibernate.dialect.SQLServerDialect
    property name="show_sql">false
    property name="">create

  18. @Adel, your database properties seems correct. However, it's hard to determine further the cause of your app's error.

    1. Hi Krams,

      Even I am getting the above error. All connection attributes are proper but still not able to login...Also maven is still not installing and showing the same error:

  19. how do i run this application from eclipse IDE?

    1. You have to install the Maven Eclipse plugin. I'm using STS (SpringSource Tool Suite) as my IDE which has m2e (Maven Eclipse plugin). But you should be able to install it as a separate plugin in your Eclipse IDE.

    2. I am using STS but it shows me many errors.
      First of all, all JSP files come with errors.
      When I deploy the project as it is, it show me errors. may jar files are not there.

  20. How do you instantiate the personService in the controller?

    private PersonService personService;

    I have made an app based in your example and the service is null inside the controller...


  21. Thanks Kram. Excellent tutorial. I am currently using this to switch between datasources

    However I don't want to update the xml/java file every time I add a new client (database). Is there anyway that we can set the data source without having to change the code?

    Any pointers is greatly appreciated.

  22. 9:37:40,866 ERROR [STDERR] SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
    19:37:40,866 ERROR [STDERR] SLF4J: Defaulting to no-operation (NOP) logger implementation
    19:37:40,866 ERROR [STDERR] SLF4J: See for further details.
    19:37:40,991 INFO [DriverManagerDataSource] Loaded JDBC driver: oracle.jdbc.driver.OracleDriver
    19:37:41,038 INFO [DispatcherServlet] FrameworkServlet 'spring': initialization completed in 844 ms
    19:37:41,053 WARN [PageNotFound] No mapping found for HTTP request with URI [/NEWMVC/persons.obj] in DispatcherServlet with name 'spring'

  23. This comment has been removed by the author.

  24. kram Excelent tutorial but i want to do this in eclipse

    1. I think the closest tutorial I got regarding your request can be seen here

      Depending on your background, I hope this won't be too complicated or foreign.

    2. Mark thank for your response and i want to Implement a DAO concept in Spring MVC using JdbcTemplate in eclipse .....give me the steps ..

  25. great article on Spring, Thanks

  26. Hi all! I've tried to compile and run and got the folowing error:

    SEVERE: Servlet /spring-jdbc threw load() exception
    java.lang.ClassCastException: org.springframework.web.servlet.DispatcherServlet cannot be cast to javax.servlet.Servlet

    What does it mean?

  27. can you develop a web application using spring mvc and jdbc for mysql.
    the ui should have a jsp page which shd contain a text area and a submit button. in text area sql queries should be typed and it should fetch the info from the db(mysql).

  28. Great Article It helped me to understand how to manage a WEB app with spring I made a few changes in order to work with mysql. Thanks

  29. Hi

    Great article.

    I get this error when trying to deploy it on jboss however.

    10:29:40,410 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/spring-jdbc]] (MSC service thread 1-1) Error configuring application listener of class org.springframework.web.context.ContextLoaderListener: java.lang.ClassNotFoundException: org.springframework.web.context.ContextLoaderListener from [Module "deployment.spring-jdbc.war:main" from Service Module Loader]

    Googled it and it says something about making modules. I see you, or other readers ahve not mentioned this / or the error. so could it be soemthing that i am doing that wrong? Please comment, if possible.

  30. superrb example.....
    works gr8....
    i tried it with mysql changing the required details.....
    it was successfull....
    thnx for this example.....

  31. SEVERE: Context initialization failed
    org.springframework.beans.factory.parsing.BeanDefinitionParsingException: Configuration problem: Failed to import bean definitions from relative location [jdbc-context.xml]
    Offending resource: ServletContext resource [/WEB-INF/applicationContext.xml]; nested exception is org.springframework.beans.factory.BeanDefinitionStoreException: Unexpected exception parsing XML document from ServletContext resource [/WEB-INF/jdbc-context.xml]; nested exception is java.lang.NoClassDefFoundError: org/aopalliance/intercept/MethodInterceptor
    at org.springframework.beans.factory.parsing.FailFastProblemReporter.error(
    at org.springframework.beans.factory.parsing.ReaderContext.error(
    at org.springframework.beans.factory.parsing.ReaderContext.error(
    at org.springframework.beans.factory.xml.DefaultBeanDefinitionDocumentReader.importBeanDefinitionResource(
    i have stuck last two day pls help any body

  32. pls any body provide fast solution

  33. This comment has been removed by the author.

  34. @krams sir pls provide fast soluation i have waiting for ur solution

  35. @Krams, Seems you forgot to put the personspage in the code.

  36. Hi Krams,

    I am getting below exception.Could you pls help me how to resolve this.

    INFO: Initializing Spring root WebApplicationContext
    [ERROR] [localhost-startStop-1 09:55:16] ( Connections could not be acquired from the underlying database!
    Feb 02, 2016 9:55:16 PM org.apache.catalina.core.ApplicationContext log

  37. I have read your blog its very attractive and impressive. I like it your blog.

    Spring online training Spring online training Spring Hibernate online training Spring Hibernate online training Java online training

    spring training in chennai spring hibernate training in chennai

  38. It was excellent and really informative.I also like composing something if my downtime. So I could find out something from your write-up. Thanks.

    Best BCA Colleges in Noida

  39. If you need your ex-girlfriend or ex-boyfriend to come crawling back to you on their knees (no matter why you broke up) you must watch this video
    right away...

    (VIDEO) Want your ex CRAWLING back to you...?

  40. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging.

    core java training in Electronic City

    Hibernate Training in electronic city

    spring training in electronic city

    java j2ee training in electronic city