Student Registration System – Spring Boot Project Guide

Concepts Covered:

  • HTML form → Spring Boot Controller
  • Save data into MySQL
  • View student list

Modules:

  • Add Student
  • View All Students

Why Build This: ðŸ‘‰ Perfect first CRUD project – covers all basics of Create and Read operations

What You’ll Learn:

  • Form handling with POST requests
  • Saving data to database
  • Retrieving and displaying data
  • Basic HTML and Bootstrap styling

Prerequisites

  • JDK 11 or higher
  • MySQL installed
  • IDE (IntelliJ IDEA, Eclipse, or VS Code)
  • Maven

Step 1: Project Setup

Create Spring Boot Project

Go to Spring Initializr and configure:

  • Project: Maven
  • Language: Java
  • Spring Boot: 3.2.x (or latest stable)
  • Dependencies:
    • Spring Web
    • Spring Data JPA
    • MySQL Driver
    • Thymeleaf (for HTML templates)

Download and extract the project.


Step 2: Configure MySQL Database

Create Database

sql

CREATE DATABASE student_db;

Configure application.properties

In src/main/resources/application.properties:

properties

# Database Configuration
spring.datasource.url=jdbc:mysql://localhost:3306/student_db
spring.datasource.username=root
spring.datasource.password=your_password

# JPA Configuration
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQLDialect

# Server Configuration
server.port=8080

Step 3: Create Entity Class

Create Student.java in com.example.demo.model package:

java

package com.example.demo.model;

import jakarta.persistence.*;

@Entity
@Table(name = "students")
public class Student {
    
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;
    
    @Column(nullable = false)
    private String name;
    
    @Column(nullable = false, unique = true)
    private String email;
    
    @Column(nullable = false)
    private String course;
    
    // Constructors
    public Student() {
    }
    
    public Student(String name, String email, String course) {
        this.name = name;
        this.email = email;
        this.course = course;
    }
    
    // Getters and Setters
    public Long getId() {
        return id;
    }
    
    public void setId(Long id) {
        this.id = id;
    }
    
    public String getName() {
        return name;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    
    public String getEmail() {
        return email;
    }
    
    public void setEmail(String email) {
        this.email = email;
    }
    
    public String getCourse() {
        return course;
    }
    
    public void setCourse(String course) {
        this.course = course;
    }
}

Step 4: Create Repository Interface

Create StudentRepository.java in com.example.demo.repository:

java

package com.example.demo.repository;

import com.example.demo.model.Student;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface StudentRepository extends JpaRepository<Student, Long> {
    // JpaRepository provides all CRUD methods automatically
}

Step 5: Create Service Layer

Create StudentService.java in com.example.demo.service:

java

package com.example.demo.service;

import com.example.demo.model.Student;
import com.example.demo.repository.StudentRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentService {
    
    @Autowired
    private StudentRepository studentRepository;
    
    // Save student
    public Student saveStudent(Student student) {
        return studentRepository.save(student);
    }
    
    // Get all students
    public List<Student> getAllStudents() {
        return studentRepository.findAll();
    }
}

Step 6: Create Controller

Create StudentController.java in com.example.demo.controller:

java

package com.example.demo.controller;

import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

@Controller
public class StudentController {
    
    @Autowired
    private StudentService studentService;
    
    // Show registration form
    @GetMapping("/")
    public String showRegistrationForm(Model model) {
        model.addAttribute("student", new Student());
        return "index";
    }
    
    // Handle form submission
    @PostMapping("/register")
    public String registerStudent(@ModelAttribute("student") Student student, Model model) {
        studentService.saveStudent(student);
        model.addAttribute("message", "Student registered successfully!");
        return "redirect:/students";
    }
    
    // Show all students
    @GetMapping("/students")
    public String viewStudents(Model model) {
        model.addAttribute("students", studentService.getAllStudents());
        return "students";
    }
}

Step 7: Create HTML Templates

Create index.html (Registration Form)

In src/main/resources/templates/index.html:

html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Student Registration</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="row justify-content-center">
            <div class="col-md-6">
                <div class="card shadow">
                    <div class="card-header bg-primary text-white">
                        <h3 class="text-center">Student Registration Form</h3>
                    </div>
                    <div class="card-body">
                        <form th:action="@{/register}" th:object="${student}" method="post">
                            <div class="mb-3">
                                <label for="name" class="form-label">Full Name:</label>
                                <input type="text" class="form-control" id="name" 
                                       th:field="*{name}" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="email" class="form-label">Email:</label>
                                <input type="email" class="form-control" id="email" 
                                       th:field="*{email}" required>
                            </div>
                            
                            <div class="mb-3">
                                <label for="course" class="form-label">Course:</label>
                                <select class="form-control" id="course" th:field="*{course}" required>
                                    <option value="">Select Course</option>
                                    <option value="Computer Science">Computer Science</option>
                                    <option value="Information Technology">Information Technology</option>
                                    <option value="Electronics">Electronics</option>
                                    <option value="Mechanical">Mechanical</option>
                                    <option value="Civil">Civil</option>
                                </select>
                            </div>
                            
                            <div class="d-grid gap-2">
                                <button type="submit" class="btn btn-primary">Register</button>
                                <a href="/students" class="btn btn-secondary">View All Students</a>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
</body>
</html>

Create students.html (Student List)

In src/main/resources/templates/students.html:

html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>All Students</title>
    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
    <div class="container mt-5">
        <div class="card shadow">
            <div class="card-header bg-success text-white">
                <h3 class="text-center">Registered Students</h3>
            </div>
            <div class="card-body">
                <div class="mb-3">
                    <a href="/" class="btn btn-primary">Add New Student</a>
                </div>
                
                <div th:if="${students.isEmpty()}" class="alert alert-info">
                    No students registered yet.
                </div>
                
                <table th:if="${!students.isEmpty()}" class="table table-striped table-hover">
                    <thead class="table-dark">
                        <tr>
                            <th>ID</th>
                            <th>Name</th>
                            <th>Email</th>
                            <th>Course</th>
                        </tr>
                    </thead>
                    <tbody>
                        <tr th:each="student : ${students}">
                            <td th:text="${student.id}"></td>
                            <td th:text="${student.name}"></td>
                            <td th:text="${student.email}"></td>
                            <td th:text="${student.course}"></td>
                        </tr>
                    </tbody>
                </table>
            </div>
        </div>
    </div>
</body>
</html>

Step 8: Run the Application


1. Make sure MySQL is running
2. Run your Spring Boot application
3. Open browser and go to: `http://localhost:8080`
4. Register some students and view the list!

Project Structure


src/
├── main/
│ ├── java/
│ │ └── com/example/demo/
│ │ ├── controller/
│ │ │ └── StudentController.java
│ │ ├── model/
│ │ │ └── Student.java
│ │ ├── repository/
│ │ │ └── StudentRepository.java
│ │ ├── service/
│ │ │ └── StudentService.java
│ │ └── DemoApplication.java
│ └── resources/
│ ├── templates/
│ │ ├── index.html
│ │ └── students.html
│ └── application.properties

Key Concepts You’ve Learned

✅ MVC Pattern: Controller → Service → Repository → Database
✅ JPA/Hibernate: Object-Relational Mapping
✅ Thymeleaf: Server-side template engine
✅ Form Handling: POST requests with @ModelAttribute
✅ Database Operations: Create and Read (CR in CRUD)
✅ Dependency Injection: @Autowired annotation


Leave a Reply

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