We’ve reached one of the most interesting topics — configuring a database connection in Spring Boot. Today we’ll dive into the ins and outs of setting up the database via the application.properties file, go over the key parameters, and practice connecting a real database. This is the core stuff — without a DB connection your app is like a library with no books.
1. What is application.properties?
You already know a bit of Java and Spring, so you probably know every app has a place where the main settings live. In Spring Boot that place is the application.properties file (or its YAML alternative application.yml). This file lets you:
- Configure the database connection.
- Control Hibernate behavior.
- Configure the connection pool.
- And much more — for example, manage logging or application profiles.
Here’s what a typical application.properties file looks like for DB setup:
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Let’s go through each of these settings one by one.
2. Main database connection settings
Database URL (spring.datasource.url)
The database URL is the string that describes how to connect to your DB. It includes:
- The protocol (for example,
jdbc:mysql). - The server address (
localhostfor a local machine). - The DB port (for example,
3306for MySQL). - The database name (
my_databasein the example).
Example for MySQL:
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
Example for PostgreSQL:
spring.datasource.url=jdbc:postgresql://localhost:5432/my_database
If your project uses another driver, you can find details in the official JDBC documentation: official JDBC documentation.
Username and password (spring.datasource.username and spring.datasource.password)
These settings are used to authenticate your app with the database. For example:
spring.datasource.username=root
spring.datasource.password=secret
If you’re using an embedded DB (like H2), the username and password might be empty, but for most real DBs they’re required.
Database driver (spring.datasource.driver-class-name)
Spring Boot usually detects the DB driver automatically based on the URL, but sometimes you need to specify it explicitly:
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
For popular DBs like MySQL and PostgreSQL, drivers are available on Maven Central. For example, for MySQL you need to add the dependency:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
3. Hibernate settings
Spring Boot uses Hibernate as the default JPA implementation, so we can tweak its behavior:
Automatic schema generation (spring.jpa.hibernate.ddl-auto)
This setting defines what should happen to your database when the app starts:
create— the database is recreated on every startup (old data is removed).create-drop— the DB is created at startup and dropped when the app stops.update— modifies the existing schema without touching data.validate— checks if the DB schema matches your entities but does not change it.none— Hibernate does nothing to the schema.
During development update is commonly used.
spring.jpa.hibernate.ddl-auto=update
But be careful: this setting can break data in production. On a production server it’s better to use validate or none.
Hibernate logs (spring.jpa.show-sql and spring.jpa.properties.hibernate.format_sql)
It’s useful during development to see the SQL queries being executed. Spring Boot lets you enable that with spring.jpa.show-sql:
spring.jpa.show-sql=true
To make queries more readable, you can also enable formatting:
spring.jpa.properties.hibernate.format_sql=true
Additional connection pool settings
Spring uses HikariCP as the connection pool by default. You can configure pool parameters like max connections, timeouts, etc.:
spring.datasource.hikari.maximum-pool-size=10
spring.datasource.hikari.idle-timeout=30000
spring.datasource.hikari.max-lifetime=1800000
Read more about available settings in the HikariCP documentation: HikariCP documentation.
4. Hands-on: Connecting your app to a MySQL database
Step 1. Add the MySQL dependency to pom.xml:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Step 2. Configure the database in application.properties:
spring.datasource.url=jdbc:mysql://localhost:3306/my_database
spring.datasource.username=root
spring.datasource.password=secret
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=create
spring.jpa.show-sql=true
spring.jpa.properties.hibernate.format_sql=true
Step 3. Run the application.
If everything is set up correctly, Spring Boot will automatically create tables for your entities. SQL queries will show up in the console because of spring.jpa.show-sql.
5. Using an embedded database (H2)
If you don’t have MySQL or PostgreSQL yet, you can test the app with H2. Just add the dependency:
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
And add these settings to application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.username=sa
spring.datasource.password=password
spring.jpa.hibernate.ddl-auto=create
spring.h2.console.enabled=true
spring.h2.console.path=/h2-console
Now you can open the H2 web console at http://localhost:8080/h2-console and inspect how your tables work.
6. Common errors and fixes
- Database connection error. Check that the DB server is running and the URL, username, and password are correct.
- SQLSyntaxErrorException. Make sure your entities are configured correctly and match the table structure.
- Driver not found. Don’t forget to add the dependency for your DB in
pom.xml.
If all this sounds like detective work, welcome to the world of DB configuration. You’ll spend a lot of time here (and that’s totally normal).
That’s it for the DB setup. Configuring connections is the first step toward building a flexible and powerful app. Now your Spring Boot project is ready to interact with real databases.
GO TO FULL VERSION