How to fetch data from database in Spring MVC

In this tutorial, you will learn how to fetch data from database in Spring MVC. Spring framework provides a class JdbcTemplate inside org.springframework.jdbc.core package under the JDBC module. And by calling its query(String sql, RowMapper<T> rowMapper) method it will return the list (in my case, it will return the Employee type list).

You need to pass your SQL statement and object of RowMapper class.

List<Employee> list = jdbcTemp.query("SELECT * FROM EMP_DETAILS", new RowMapper<Employee>());

Steps to follow

To fetch data from database in Spring MVC application, you must follow these steps.

  1. Register your front controller and enable Spring MVC feature.
  2. Register your database credentials like URL, username, and password.
  3. Execute the SQL statement and store in a list.
  4. Iterate the list on JSP.

Technologies Used

Find the list of all technologies used in this application.

  • Eclipse Oxygen
  • Tomcat 9
  • JDK 8
  • Maven 3
  • Spring5.0.2.RELEASE
  • MySQL Database
  • JSTL 1.2

Dependencies Required

To retrieve data from database in Spring MVC application, you need the following JARs.


		<!-- spring mvc dependency -->

		<!-- spring jdbc dependency -->

		<!-- mysql databse connector -->



Project Structure

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

How to fetch data from database in Spring MVC

Model Class

Create an Employee model class and mention its attribute like name, salary, department, etc.
package org.websparrow.model;

public class Employee {

	// Getters and Setters...
	private String name;
	private String designation;
	private String dept;
	private int salary;
	private String location;


DAO Classes

Create EmployeeDao interface and define the empList() method of Employee type.
package org.websparrow.dao;

import java.util.List;

import org.websparrow.model.Employee;

public interface EmployeeDao {

	public List<Employee> empList();


Create an implementation class EmployeeDaoImpl that implements EmployeeDao.
package org.websparrow.dao;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.sql.DataSource;

import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.websparrow.model.Employee;

public class EmployeeDaoImpl implements EmployeeDao {

	private JdbcTemplate jdbcTemp;

	public EmployeeDaoImpl(DataSource dataSource) {
		jdbcTemp = new JdbcTemplate(dataSource);

	public List<Employee> empList() {
		List<Employee> list = jdbcTemp.query("SELECT * FROM EMP_DETAILS", new RowMapper<Employee>() {

			public Employee mapRow(ResultSet rs, int rowNum) throws SQLException {
				Employee emp = new Employee();


				return emp;


		return list;


Register Front Controller & Enable Spring MVC
package org.websparrow.config;


public class FontControllerConfig extends AbstractAnnotationConfigDispatcherServletInitializer {

	protected Class<?>[] getRootConfigClasses() {

		return new Class[] { WebMvcConfig.class };

	protected Class<?>[] getServletConfigClasses() {

		return null;

	protected String[] getServletMappings() {
		return new String[] { "/" };
package org.websparrow.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.datasource.DriverManagerDataSource;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.websparrow.dao.EmployeeDao;
import org.websparrow.dao.EmployeeDaoImpl;

@ComponentScan(basePackages = { "org.websparrow.controller", "org.websparrow.dao" })
public class WebMvcConfig {

	public InternalResourceViewResolver viewResolver() {

		InternalResourceViewResolver vr = new InternalResourceViewResolver();

		return vr;

	public DriverManagerDataSource getDataSource() {

		DriverManagerDataSource ds = new DriverManagerDataSource();

		return ds;

	public EmployeeDao getEmployeeDao() {
		return new EmployeeDaoImpl(getDataSource());


The getDataSource() method returns a configured DataSource bean. You may have to change the database URL, username and password according to your environments. And getEmployeeDao() method returns an implementation of the EmploueeDao interface, which is the EmployeeDaoImpl class. This bean will be injected to the controller class.

Controller Class

Create a controller class that handles the user request and @Autowired annotation inject an instance of the EmployeeDao implementation into this controller automatically.
package org.websparrow.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;
import org.websparrow.dao.EmployeeDao;
import org.websparrow.model.Employee;

public class MyController {

	private EmployeeDao empDao;

	@RequestMapping(value = "/fetch")
	public ModelAndView listEmployee(ModelAndView model) throws IOException {

		List<Employee> listEmp = empDao.empList();
		model.addObject("listEmp", listEmp);

		return model;

JSP Page

On the JSP, iterate the list returned by controller class using JSTL.

<%@ taglib uri="" prefix="c"%>
	<h2>How to fetch data from database in Spring MVC</h2>

	<table border="1">

		<th>Sr No</th>

		<c:forEach var="emp" items="${listEmp}" varStatus="status">
				<td>${status.index + 1}</td>




Run your application and hit the below URL in your browser address bar.

URL: localhost:8090/spring-mvc-fetch-data/fetch

You will get the following result.

How to fetch data from database in Spring MVC


  1. Spring @Autowired annotation example
  2. Data access with JDBC
  3. Class JdbcTemplate

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.