DEV Community

Navneet Karnani
Navneet Karnani

Posted on • Originally published at blog.mandraketech.in on

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"]}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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/testdbspring.datasource.username=rootspring.datasource.password=rootpassword
Enter fullscreen mode Exit fullscreen mode

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;@SpringBootApplicationpublic 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)); } } }}
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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.
Enter fullscreen mode Exit fullscreen mode

About the Author

The Author, Navneet Karnani, began coding with Java in 1997 and has been a dedicated enthusiast ever since. He strongly believes in the "Keep It Simple and Stupid" principle, incorporating this design philosophy into all the products he has developed.

Navneet works as a freelancer and is available for contracts, mentoring, and advisory roles related to technology and its application in software product development.

Additionally, Navneet serves as a visiting faculty member at FLAME University.

Driven software engineer (Java since 1997) with a hands-on passion for building impactful tech products. Possesses over 25 years of experience crafting solutions to complex business and technical challenges.

]]>

Top comments (0)

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay