Configuring A Database For Spring Boot

Sat, 09/09/2017 - 10:03

In the previous post we built a "hello world" application on Spring Boot. In this post we will be configuring the same application for a database connection. If you have not followed the previous tutorial, I highly encourage to do it, because this post is the logical continuation of the previous one. Now that we have our basic app running from the previous post, we can add two more Maven dependencies to pom.xml, because after all we need to be able to interact with a database server. In our case it is going to be the MySQL. We need MySQL JDBC Driver that will allow us to connect easily to live MySQL data, so we add "mysql-connector-java" dependency to pom.xml. In order to carry out the persistent related actions to and from the database  we will use the Spring Data JPA, which in its turn can be added through "spring-boot-starter-data-jpa" maven dependency. The complete pom.xml file will look like this.

<project xmlns="" xmlns:xsi="" xsi:schemaLocation="">
  <name>Demo App</name>
  <description>Spring Boot Demo App</description>

Additionally, we need to configure the database connection in the file. I have a MySQL server installed locally on my ubuntu machine, so I am going to connect to it. If you do not have it, you can easily install it by typing in your terminal the following installation command

$ sudo apt-get install mysql-server

In the process of the installation a dialog box will show up asking to type New password for the MySQL "root" user. The "root" will be your username and whatever you type will be your password to authenticate when connecting to the database. I typed "aralmighty" as my password so I am going to use that password when configuring my file. I will follow the instruction given in the official docs of Spring Boot, which can be found here.



The first two lines are left from the previous post and do not need any explanation. The next four are standing for the DataSource configuration, controlled by external configuration properties in spring.datasource.*. The general format for the jdbc connection url is


My MySQL server runs on my local machine using the default port number (3306). If I had PostgreSQL, it would listen on port 5432 by default and the jdbc connection url would be "jdbc:postgresql://localhost:5432/demo_db". In the case of PostgreSQL the driver class name is  


which could be found in its specific dependency jar for PostgreSQL connection that we did not choose to include into our pom.xml dependences, because we use MySQL here =D . 

The "demo_db" database name, as you have perhaps noticed in our configuration must still be created. The next 4 lines are used to explicitly configure JPA settings through  spring.jpa.* properties that for more details I refer you here. To create the "demo_db" database we go to terminal and type

$ mysql -u root -p

After typing the authentication password (in my case it is "aralmighty" as I mentioned above), go ahead and create the database.

mysql> CREATE DATABASE demo_db;

and now if we type "SHOW DATABASES;" we should be able to see the created "demo_db" database in the list of databases.

| Database           |
| information_schema |
| demo_db            |
| mysql              |
| performance_schema |
| phpmyadmin         |
5 rows in set (0.00 sec)

At this point our configuration for connecting the demo_db database is complete and we are good to stop here. In my future tutorials I will use this configuration to demonstrate the mapping of entity classes to the tables of the demo_db database.