How to Connect to MySQL in Spring Boot: Simple Guide
To connect to MySQL in Spring Boot, add the
spring-boot-starter-data-jpa and mysql-connector-java dependencies, then configure your database details in application.properties. Spring Boot will auto-configure the connection using these settings.Syntax
To connect Spring Boot to MySQL, you need to add dependencies and configure connection properties.
- Dependencies: Include
spring-boot-starter-data-jpafor JPA support andmysql-connector-javafor MySQL driver. - Configuration: Set database URL, username, password, and driver class in
application.properties. - Entity and Repository: Define JPA entities and repositories to interact with the database.
properties
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
runtimeOnly 'mysql:mysql-connector-java'
}
# application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/your_database
spring.datasource.username=your_username
spring.datasource.password=your_password
spring.datasource.driver-class-name=com.mysql.cj.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=trueExample
This example shows a simple Spring Boot application connecting to MySQL, defining an entity, and saving data.
java
package com.example.demo; import jakarta.persistence.Entity; import jakarta.persistence.Id; import org.springframework.boot.CommandLineRunner; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.stereotype.Repository; import org.springframework.beans.factory.annotation.Autowired; @SpringBootApplication public class DemoApplication implements CommandLineRunner { @Autowired private UserRepository userRepository; public static void main(String[] args) { SpringApplication.run(DemoApplication.class, args); } @Override public void run(String... args) throws Exception { User user = new User(); user.setId(1L); user.setName("Alice"); userRepository.save(user); System.out.println("User saved: " + user.getName()); } } @Entity class User { @Id private Long id; private String name; public Long getId() { return id; } public void setId(Long id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } } @Repository interface UserRepository extends JpaRepository<User, Long> {}
Output
User saved: Alice
Common Pitfalls
Common mistakes when connecting Spring Boot to MySQL include:
- Forgetting to add the
mysql-connector-javadependency, causing driver not found errors. - Incorrect JDBC URL format; it must start with
jdbc:mysql://and include the database name. - Using wrong driver class name; the correct one is
com.mysql.cj.jdbc.Driverfor recent MySQL versions. - Not setting
spring.jpa.hibernate.ddl-autoproperly, which can cause schema issues. - MySQL server not running or wrong credentials causing connection failures.
properties
/* Wrong JDBC URL example */ spring.datasource.url=jdbc:mysql://localhost/your_database /* Correct JDBC URL example */ spring.datasource.url=jdbc:mysql://localhost:3306/your_database
Quick Reference
Summary tips for connecting Spring Boot to MySQL:
- Always use
spring-boot-starter-data-jpaandmysql-connector-javadependencies. - Configure
application.propertieswith correct URL, username, password, and driver. - Use
com.mysql.cj.jdbc.Driveras the driver class name. - Set
spring.jpa.hibernate.ddl-autotoupdatefor automatic schema updates during development. - Ensure MySQL server is running and accessible.
Key Takeaways
Add spring-boot-starter-data-jpa and mysql-connector-java dependencies to your project.
Configure database connection properties correctly in application.properties.
Use the correct JDBC URL format and driver class name for MySQL.
Define JPA entities and repositories to interact with the database.
Check MySQL server status and credentials to avoid connection errors.