Spring MVC Database Connectivity using XML Configuration

In this article, you will learn Spring MVC database connectivity using XML configuration. Spring framework provides DriverManagerDataSource class to manage the database connection in its JDBC module. DriverManagerDataSource have fields like driverClassName, URL, username, and password to create the database connection object.

As mentioned in the headline, we going to create database connection object using XML configuration and check if the connection is not null then return the SUCCESS message otherwise FAILURE message.

Similar Post: Spring MVC Database Connectivity Example using Annotation and Java Based Configuration

Dependency Required

Find the list of dependencies used in this example.


		<!-- spring mvc dependency -->

		<!-- spring jdbc dependency -->

		<!-- mysql databse connector -->


Technology Used

Find the list of all technologies used in this application.

  • Eclipse Oxygen
  • Tomcat 9
  • JDK 8
  • Maven 3
  • Spring5.0.2.RELEASE

Project Structure

Final project structure of our application in Eclipse IDE will look like as follows.

Spring MVC Database Connectivity using XML Configuration

Configure Front Controller

Add the Spring MVC front controller i.e. DispatcherServlet to your web.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID"

Create Controller

To check database connection, create a DbController class that implements Controller interface which has one method handleRequest(request, response) and its return type is ModelAndView.

package org.websparrow.controller;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.sql.DataSource;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.Controller;

public class DbController implements Controller {

	DataSource dataSource;

	public void setDataSource(DataSource dataSource) {
		this.dataSource = dataSource;

	public ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

		Map<String, String> map = new HashMap<String, String>();

		if (dataSource.getConnection() != null) {
			map.put("msg", "Database connection established successfully.");

		} else {
			map.put("msg", "Failed to connect database.");


		return new ModelAndView("index", map);


Map Action Request

Map the controller class to the action name and set the database credentials into the DriverManagerDataSource property.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"

	<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
		<property name="prefix" value="/" />
		<property name="suffix" value=".jsp" />
	<bean id="dmds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
		<property name="driverClassName" value="com.mysql.jdbc.Driver" />
		<property name="url" value="jdbc:mysql://localhost:3306/websparrow" />
		<property name="username" value="root" />
		<property name="password" value="" />
	<bean name="/checkConnection.htm" class="org.websparrow.controller.DbController">
		<property name="dataSource" ref="dmds"></property>


View Components

Create the JSP pages that visible on the user machine.

	<h2>Spring MVC Database Connectivity Example using XML configuration</h2>
	<b>Message:</b> ${msg}


Finally, everything was completed. Run your project and will get the following result.

Spring MVC Database Connectivity Example using Annotation and Java Based Configuration


  1. Spring MVC Database Connectivity Example using Annotation and Java Based Configuration
  2. Spring 5 MVC Hello World using XML configuration
  3. Spring Web MVC
  4. Front Controller- Dispatcher Servlet

Similar Posts

About the Author

Atul Rai
I love sharing my experiments and ideas with everyone by writing articles on the latest technological trends. Read all published posts by Atul Rai.