Configure database in Spring Boot project using JPA Starter dependency

To use JPA in any project, we need to declare its configuration including the database it will use, the username and password for it to connect to, etc. With the Spring Boot project, this is no exception if you use JPA Starter dependency. So in this tutorial, I would like to guide you to configure the database in Spring Boot project with JPA Starter dependency!

First, I will create a Spring Boot project with the JPA Starter dependency. Depending on the database system you want to use, add the corresponding JDBC Driver dependency. I will use JPA with the database is MySQL so I will also add MySQL Starter dependency. The project is as follows:

Configure database in Spring Boot project using JPA Starter dependency

If someone does not know how to create Spring Boot project in Spring Tool Suite, you guys can refer to this tutorial.

Now, if you run this project, you will encounter the following error:

What is the cause? That’s because we have not configured the database for JPA Starter dependency yet.
To resolve this error, you need to do the following:

First of all, we must have a database first.

You create a database using a database system you want like MySQL, Oracle, …
In this example, I will create a database named springboottutorial in the MySQL server.

Next, we will configure the database information for Spring Boot.
To do this, open the application.properties file in your project’s src /main/resources directory and configure the following:

In this example, I configure the following:

In the Spring Tool Suite, when you open the application.properties file, you can display all of the properties that Spring Boot supports by entering “spring.” and then pressing Ctrl + Space.
For example:

Configure database in Spring Boot project using JPA Starter dependency

Now try running your Spring Boot project again, you will see no more errors.

Add Comment