17 Jan 2025 · 3 min read
Java, SpringBoot and MySQL in devcontainer
In this tutorial, we’ll walk through the steps to create a simple Java Spring console application that connects to a MySQL database running in a Docker container, creates a “test” table, and lists all the tables. We’ll use a devcontainer.json file to set up the development environment and Docker Compose to define the services.
Step 1: Set Up the Environment
Create devcontainer.json file
{
"name": "Java Dev Environment",
"dockerComposeFile": "docker-compose.yml",
"service": "java",
"workspaceFolder": "/workspace",
"extensions": [
"vscjava.vscode-java-pack",
"pivotal.vscode-spring-boot"
]
}
Create docker-compose.yml file
services:
java:
image: mcr.microsoft.com/vscode/devcontainers/java:0-11
volumes:
- .:/workspace
command: sleep infinity
mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: rootpassword
MYSQL_DATABASE: testdb
Step 2: Develop the Java Application
Create a new Spring Boot project
Use Spring Initializr to generate a new Spring Boot project with the following dependencies:
-
Spring Web
-
MySQL Driver
Configure the application properties
In src/main/resources/application.properties, add the following configuration:
spring.datasource.url=jdbc:mysql://mysql:3306/testdb
spring.datasource.username=root
spring.datasource.password=rootpassword
Create a Main class
package com.example.demo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
@SpringBootApplication
public class DemoApplication implements CommandLineRunner {
@Autowired
private DataSource dataSource;
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
try (Connection conn = dataSource.getConnection();
Statement stmt = conn.createStatement()) {
// Create a test table
stmt.executeUpdate("CREATE TABLE IF NOT EXISTS test (id INT AUTO_INCREMENT, name VARCHAR(255), PRIMARY KEY (id))");
// List all tables
ResultSet rs = stmt.executeQuery("SHOW TABLES");
while (rs.next()) {
System.out.println(rs.getString(1));
}
}
}
}
Step 3: Run the Application
Open the Codespace
Open the project in your Codespace. The devcontainer.json will set up the development environment and start the containers automatically.
Run the Spring Boot application
In the terminal, navigate to the project directory and run:
./mvnw spring-boot:run
Step 4: Verify the Connection and Execution
Check the terminal output to verify that the connection to the MySQL database was successful and that the “test” table was created and listed.
Built using Microsoft Copilot, with the following prompt:
Can you create a tutorial for me for the following:
Create a simple Java Spring console application that connects
to a MySQL database, running in a Docker container,
create a "test" table, and list all the tables.
Set up the environment:
Use a devcontainer.json file to use a docker-compose.yml
file to define the Java dev environment and MySQL.
Develop the Java application:
Create a Java Spring Boot application that establishes a
connection to the MySQL database using JDBC.
Run the application:
Build and run the Java application within the Codespace.
Verify the successful connection to the database and the
execution of queries.