Displaying Database Data in a Dropdown List with SpringMVC

This tutorial demonstrates how to populate a dropdown select element with data retrieved from a MySQL database using SpringMVC.

Environment

  • Tomcat 8.5
  • MySQL 8.0
  • Eclipse
  • SpringMVC Framework

Database Query Layer

First, create a DAO class to handle database operations for retrieving class information.

public class GradeDao {
    public List<Grade> fetchAllGrades() {
        List<Grade> gradeCollection = new ArrayList<Grade>();
        Grade gradeEntity = new Grade();
        
        Connection dbConnection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        
        dbConnection = DatabaseConnector.getConnection();
        String query = "SELECT grade_id, grade_name, instructor_id FROM grade_info";
        
        try {
 statement = dbConnection.prepareStatement(query);
            resultSet = statement.executeQuery();
            
            while (resultSet.next()) {
                gradeEntity = new Grade();
                gradeEntity.setGradeId(resultSet.getString("grade_id"));
                gradeEntity.setGradeName(resultSet.getString("grade_name"));
                gradeEntity.setInstructorId(resultSet.getString("instructor_id"));
                gradeCollection.add(gradeEntity);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DatabaseConnector.closeResources(dbConnection, statement, resultSet);
        }
        
        return gradeCollection;
    }
}

Controller Layer

Create a controller to invoke the DAO and pass the retrieved data to the view layer.

@Controller
public class EnrollmentController {

    @RequestMapping("/studentEnrollment.form")
    public ModelAndView loadEnrollmentPage() {
        List<Grade> gradeCollection = new ArrayList<Grade>();
        GradeDao gradeDao = new GradeDao();
        
        gradeCollection = gradeDao.fetchAllGrades();
        
        ModelAndView modelView = new ModelAndView();
        modelView.addObject("gradeList", gradeCollection);
        modelView.setViewName("student/enrollmentForm");
        
        return modelView;
    }
}

The view name must correspond to the actual JSP file path in your webappp directory.

View Layer (JSP)

Implement the dropdown element in your JSP page using JSTL to iterate over the collection.

<label for="gradeSelection">Grade:</label>
<select style="width:240px;" class="grade-select" id="gradeSelection" name="gradeSelection">
    <option value="">-- Select Grade --</option>
    <c:forEach items="${gradeList}" var="gradeItem">
        <option value="${gradeItem.gradeId}">${gradeItem.gradeName}</option>
    </c:forEach>
</select>

Required Dependencies

To enable JSTL functionality in your JSP pages, add the following declaration at the top of your JSP file:

<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>

Ensure the following JAR files are included in your project's WEB-INF/lib directory:

  • jstl-1.2.jar
  • taglibs-standard-impl-1.2.5.jar

Once configured, refresh your browser to see the dropdown populated with grade data from the database.

Tags: JavaWeb SpringMVC MySQL JSP JSTL

Posted on Tue, 26 May 2026 22:46:34 +0000 by hhisc383