3.1 Lesson Outcomes
After completing this lesson, learners will be able to:
- Connect Java applications to MySQL databases using JDBC.
- Create and manage REST APIs using Java.
- Implement CRUD operations using HTTP methods.
- Build REST controllers using Spring Boot.
- Use Docker to deploy MySQL databases.
3.2 Overview
REST APIs allow applications to communicate over the internet using HTTP methods such as:
- GET,
- POST,
- PUT,
- and DELETE.
Java developers commonly use REST APIs to create:
- web services,
- enterprise systems,
- mobile application backends,
- and cloud-based applications.
This lesson introduces learners to practical REST API development using Java and Spring Boot.
Learners will also connect Java applications to MySQL databases using JDBC and deploy MySQL using Docker containers.
Understanding REST APIs is important because modern software systems communicate using APIs and web services.
Case Study / Scenario
You are a Java developer at a South African online learning platform. The company wants a Course Management System with the following requirements:
- Courses must be stored in a MySQL database.
- The system must support CRUD operations using a REST API.
- Docker must be used to deploy MySQL for development.
- Java and Spring Boot must be used for development.
PA0101 — Get Started with JDBC
JDBC (Java Database Connectivity) allows Java applications to connect to databases.
Java Example:
import java.sql.*;
public class JDBCExample {
public static void main(String[] args) {
String url = “jdbc:mysql://localhost:3306/course_db”;
String user = “root”;
String password = “password”;
try (Connection conn =
DriverManager.getConnection(url, user, password)) {
System.out.println(“Connected to MySQL successfully!”);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Expected Output:
Connected to MySQL successfully!
Practical Activity
Learners must:
- connect Java applications to MySQL,
- create JDBC connections,
- and display successful connection messages.
PA0102 — Map Course Entity and Populate Data with SQL
SQL is used to create and populate database tables.
SQL Example:
CREATE TABLE Course (
id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
description VARCHAR(255),
duration INT
);
INSERT INTO Course (name, description, duration)
VALUES (‘Java Basics’, ‘Intro to Java’, 10),
(‘Spring Boot’, ‘Build REST APIs’, 15);
Practical Activity
Learners must:
- create database tables,
- insert course records,
- and verify database data.
PA0103 — Create Repository to Manage Entity and Integrate with GET REST API
Spring Data JPA simplifies database operations.
Java Example:
import org.springframework.data.jpa.repository.JpaRepository;
public interface CourseRepository
extends JpaRepository<Course, Integer> {
}
Practical Activity
Learners must:
- create repository interfaces,
- manage entities,
- and integrate repositories into REST applications.
PA0104 — Build REST API to Retrieve Details of a Course — GET
GET requests retrieve data from the server.
Java Example:
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping(”/courses”)
public class CourseController {
private final CourseRepository repo;
public CourseController(CourseRepository repo) {
this.repo = repo;
}
@GetMapping
public List getAllCourses() {
return repo.findAll();
}
}
Expected JSON Output:
[
{“id”:1,“name”:“Java Basics”,“description”:“Intro to Java”,“duration”:10},
{“id”:2,“name”:“Spring Boot”,“description”:“Build REST APIs”,“duration”:15}
]
Practical Activity
Learners must:
- create GET endpoints,
- retrieve database data,
- and return JSON responses.
PA0105 — Build REST API to Create a New Course — POST
POST requests create new resources.
Java Example:
@PostMapping
public Course createCourse(@RequestBody Course course) {
return repo.save(course);
}
Practical Activity
Learners must:
- create POST endpoints,
- save database records,
- and return created resources.
PA0106 — Build REST API to Update a Course — PUT
PUT requests update existing resources.
Java Example:
@PutMapping(”/{id}”)
public Course updateCourse(
@PathVariable int id,
@RequestBody Course courseDetails) {
Course course = repo.findById(id).orElseThrow();
course.setName(courseDetails.getName());
course.setDescription(courseDetails.getDescription());
course.setDuration(courseDetails.getDuration());
return repo.save(course);
}
Practical Activity
Learners must:
- create PUT endpoints,
- update records,
- and save modified data.
PA0107 — Build REST API to Delete a Course — DELETE
DELETE requests remove resources.
Java Example:
@DeleteMapping(”/{id}”)
public void deleteCourse(@PathVariable int id) {
repo.deleteById(id);
}
Practical Activity
Learners must:
- create DELETE endpoints,
- remove records,
- and verify deletion.
PA0108 — Install Docker
Docker simplifies software deployment using containers.
Docker Installation Steps:
- Download Docker from:
https://www.docker.com - Install Docker for your operating system.
- Verify Docker installation:
docker –version
Practical Activity
Learners must:
- install Docker,
- verify installation,
- and execute Docker commands.
PA0109 — Use Docker to Launch MySQL
Docker can deploy MySQL quickly using containers.
Docker Example:
docker run –name mysql-container
-e MYSQL_ROOT_PASSWORD=password
-e MYSQL_DATABASE=course_db
-p 3306:3306
-d mysql:8
Access MySQL:
docker exec -it mysql-container mysql -uroot -p
Practical Activity
Learners must:
- launch MySQL containers,
- connect to MySQL,
- and verify running databases.
AK0101 — Principles and Functionalities of REST API in Java
REST API Feature — Function
GET — Retrieve data
POST — Create data
PUT — Update data
DELETE — Remove data
JDBC — Connect Java to databases
Docker — Deploy containerized applications
Practical Activity
Learners must:
- explain REST API functionality,
- identify HTTP methods,
- and apply REST API principles.
IAC0101 — Expected Results with REST API in Java are Achieved
Expected Results
- Java applications connect successfully to MySQL.
- CRUD operations function correctly.
- REST APIs return valid JSON responses.
- Docker containers execute successfully.
- Course data is managed successfully.
3.5 Key Notes / Summary
- REST APIs allow communication using HTTP methods.
- JDBC connects Java applications to databases.
- Spring Boot simplifies REST API development.
- CRUD operations include:
GET,
POST,
PUT,
and DELETE. - Docker simplifies deployment and development.
- REST APIs commonly return JSON responses.