Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Connecting a MySQL Database | Working with Databases
Spring Boot Backend
course content

Contenido del Curso

Spring Boot Backend

Spring Boot Backend

1. Backend Development Basics
2. Spring Boot Basics
3. RESTful API
4. Working with Databases
5. Testing Backend Applications

bookConnecting a MySQL Database

Now we are going to connect the database to our project, and you’ll see how easy it is to do. I will be using IntelliJ IDEA Ultimate to connect the database to our project.

Getting Started

The first thing you need to do is download the MySQL database. There’s a great article that provides instructions for this.

Once you have downloaded the MySQL database, you can go directly to IntelliJ IDEA and add the dependency for our MySQL database.

Connecting to the Database

IntelliJ IDEA offers built-in tools for working with databases, which greatly simplify connecting to and interacting with MySQL.

To get started, open the Database window by clicking the tab on the right, or by navigating to View > Tool Windows > Database in the menu.

Next, click the + icon and select Data Source > MySQL.

After that, enter the required connection information: specify the Host (localhost or the server's IP address), Port (default is 3306), and your credentialsUser (root) and Password (the one you set when downloading the database).

Once the configuration is complete, click Test Connection to verify the database connection. If everything is correct, you should see a window like this:

Creating a Table

To create a table in a MySQL database, we can do this directly in the dedicated database console:

In this console, we write the SQL command for the database:

You then need to execute this command by selecting it from the menu and clicking Execute:

Now, let's create a table for our database, and we’ll name it books.

As you can see, the table was successfully created, and we can now open it to view its contents.

Configuring the Configuration File for the Project

To work with the database in a Spring Boot project, you need to configure the necessary parameters. In the src/main/resources/application.properties file, specify the following settings:

The value for spring.datasource.url specifies the path to the database and consists of the protocol jdbc:mysql://, followed by localhost if the database is running on the local machine, the port (default is 3306), and the name of your database, for example, my_database (which we created earlier). You can find this information here:

The field spring.datasource.username contains the username you use to connect to the database, such as root. For spring.datasource.password, enter the password that was set when configuring MySQL.

Finally, spring.datasource.driver-class-name should always be com.mysql.cj.jdbc.Driver for MySQL, which specifies the driver being used.

Summary

We have explored how to connect a database to our project. In this section, we will also discuss how to insert, retrieve, update, and delete data in the database, but this time in code. We will be enhancing our REST API that we created in the previous section!

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 4. Capítulo 2
some-alt