Spring MVC and Spring JDBC Example

This tutorial shows an example on how MVC(Model, View, Controller) works in Spring 3.x. In this tutorial you will also find how JDBC works with Spring MVC. We will also see how annotation like @Autowired works in Spring. You will also see how datasource is configured in Spring. This example shows how to read the database configurations from properties file. You may also read Spring MVC and JDBC CRUD Example

Spring MVC and JDBC CRUD with zero XML


Once you finish the example and run the example you will see the below outputs in the browser

When welcome screen appears

spring mvc and jdbc example

When you click on the ‘Add New Item’ link

spring mvc and jdbc example

When you click ‘Add Item’ button without any input

spring mvc and jdbc example

spring mvc and jdbc example

Once the item gets successfully added

spring mvc and jdbc example

Well, now you have got an idea what you will get after the completion of this tutorial.

So before begin coding for this tutorial please make sure the below things you have.

Prerequisites

JDK 1.6.0_43
Tomcat Server v7.0
Eclipse Helios/Juno/Kepler
JAR dependency

spring mvc and jdbc example

spring mvc and jdbc example
Now we will start our coding for the example. First create a dynamic web project in Eclipse and put the jar files under WEB-INF/lib directory.

Create jdbc.properties file and put it under WEB-INF directory

 

Create a MySQL table

 

The deployment descriptor file – WEB-INF/web.xml

 

Below is the WEB-INF/applicationContext.xml file

 

Spring’s dispatcher file – WEB-INF/dispatcher-servlet.xml

 

index.jsp file under Webcontent is used to show all the items from the database table

add.jsp file under Webcontent is used to add an item

 

Model class

 

Spring Controller – WelcomeController.java. Look this file annotated with @Controller to make it a controller.

 

Spring DAO

 

Spring Service

 

RowMapper – represent each model value as a row

 

ResultSet extractor – populate model with ResultSet

 

RowMapper – represent each model value as a row

 

Utils.java class which contains all the utility methods fo the application

 

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

6 thoughts on “Spring MVC and Spring JDBC Example

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.