Spring MVC JDBCTemplate complete Application

Spring MVC JDBCTemplate complete Application

In this tutorial we will create Spring MVC JDBCTemplate complete Application means this application will have Logger, read label from properties files, separate data base properties file, static page folder to store js,css,html file etc, data base call, Internationalization, service layer, dao layer and data base connection either from JBOSS server using JNDI OR locally configuration to connect to the database. Please follow below steps to create this application:

Tools needed:

  • Eclipse ( We are using eclipse Kepler. You could also download eclipse from eclipse.org/downloads)
  • MySQL data base (Install MySQL Community Server (GPL) version in your system : MySQL Community Server). We are using version 5.6 ( If you are not sure how to install it please use this link : Install MySQL server )
  • Maven 3.0.4

Step 1. Create table to MySQL database: Please use below script to create table and insert sample data for test to the MySQL database:

Step 2: Insert test data script:

Step 3: Create Maven project name: SpringMVCJDBCTemplateTutorial. Below is final project structure:

Spring MVC JDBCTemplate complete Application

  • webapp folder:

Spring MVC JDBCTemplate complete Application

Step 4:  pom.xml file:

Step 5: dispatcher-servlet.xml

dispatcher-servlet_use_jboss_jndi.xml: Use this property file if you want to read data source configuration from application server. By default configuration has been done of JBoss application server. To use this file first you will have to create data source on application server then use data source JNDI name and inject inside bean:

Step 6: log4j.xml:

database.properties file ( Replace jdbc.url, username, password from your configuration)

message_en.properties file: Use this file to keep all your static properties values, this will also work if you want to implement internationalization default locale I kept is English and based on your need create separate file for another locale.

Step 7: web.xml file:

Step 8: SpringMVCController.java

IPersonDAO.java:

PersonDAOImpl.java:

Person.java:

IPersonService.java:

PersonServiceImpl.java:

 

Step 9: Once you copy all files in your project then run this project by Right click on project –> Run As –> Run on Server you will see below page first welcome page:

Spring MVC JDBCTemplate complete Application

Step 10: To test data base connection and fetch sample data please enter below URL you will output as below:

Spring MVC JDBCTemplate complete Application

Step 11: To test if logger is working and write log on console including it create log file in “C:/Javahonk/Log4jXML.log” also reading message label from properties file please enter below URL you will see below output:

Spring MVC JDBCTemplate complete Application

  • Message on console:

Spring MVC JDBCTemplate complete Application

  • Check C:/Javahonk folder you will see Log4jXML.log file with log information:

Spring MVC JDBCTemplate complete Application

 

Step 12: To test JSON data to user as RESTFul web server please use below URL as you will see below we are passing two parameter in URL and reading its value on the server:

Spring MVC JDBCTemplate complete Application

  • That’s it for more information to create Spring MVC JDBCTemplate application please read spring tutorial here

download Download Project: SpringMVCJDBCTemplateTutorial

One thought on “Spring MVC JDBCTemplate complete Application

Leave a Reply

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