JDBC Tutorial - Connecting to a MySQL Database Running on Docker

1.) Overview In this article, we will demonstrate how we can run a MySQL instance on docker and connect a Java application on it using JDBC....

1.) Overview

In this article, we will demonstrate how we can run a MySQL instance on docker and connect a Java application on it using JDBC.

While this exercise is done using a MySQL instance running on Docker, it can be run in a locally installed MySQL database as well.

2.) Prerequisites

Prior knowledge of existing technologies such as virtualization, eclipse ide is needed to follow this exercise. For those who are not familiar with these technologies, I recommend visiting the reference section below that points to various tutorials I have written related to it.
  • Eclipse IDE
  • GIT
  • MySQL Workbench Community
  • Docker

2.1) Running MySQL on Docker

Docker is a virtualization tool that we can use to run an application such as MySQL without actually installing it.

To install Docker, follow the guide in the link in the reference section.

To run docker, open your terminal and execute:
docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD=kerri -e MYSQL_DATABASE=catalog mysql

2.2) MySQL Workbench Installation

WorkBench installation is straight-forward for Windows, you just have to download the installer from the link below and connect to your database.

On the other hand, there are some configurations/installations that you need to perform in Debian machines to install it. See the link in the reference section below.

Once the WorkBench is running you can then connect to your database with the password you entered when you run MySQL with docker. In our case, we used 'kerri'.

By default, the database catalog will be created. Double click on it in the Schema tab so that it is selected and then press the "Create a new SQL tab..." button and enter the script in "src/main/resources/1 - schema.sql" to create our product table.

3.) Cloning the Project

You can either clone the project using the Eclipse EGIT plugin or via terminal.

Terminal: git clone

4.) Before Running the Tests

Note that we need to change the server configuration in the java class.
 - JDBC - JdbcTemplateMysqlDemo

4.1) What to Change?

1. Change the IP address of the database server. In my case, I'm running on
2. Change the username and password depending on the value you set when you installed your server.

5.) Running the Tests

There are various tests that are demonstrated in this project.

5.1) JdbcTemplateMysqlDemo

This class connects to the MySQL instance and execute CRUD operations.

7.) References

Post a Comment Default Comments


Are you looking for freelancers in the Philippines? Get in touch.


If you like what I do, you can support this channel by buying me a coffee. I would be grateful for your contribution! Your donations will help me create more FREE online courses and learning materials for programmers and remote workers.