LearnAI ToolsCareerPractice BuildsPlayContact
Lesson 922 min read

SQL Database Driver Dependencies

Learn the actual JDBC driver artifacts you pair with starter-data-jpa or starter-jdbc: MySQL, PostgreSQL, H2, and SQL Server, and how to configure spring.datasource.url for each.

Introduction

In the previous lesson, both starter-data-jpa and starter-jdbc needed a `DataSource` to actually connect to a database — but neither of those starters includes the driver for a specific database vendor. This lesson covers the four JDBC drivers you will use most often, each published as its own separate dependency.

Drivers Are Separate From Starters

A JDBC driver is the vendor-specific library that knows how to actually speak a particular database's wire protocol. Spring Boot's persistence starters are intentionally database-agnostic — they work with any driver — so you must add the correct driver dependency yourself, alongside starter-data-jpa or starter-jdbc, not instead of it.

Two Dependencies, Not One

A typical database-backed Spring Boot app needs BOTH a persistence starter (spring-boot-starter-data-jpa or spring-boot-starter-jdbc) AND a database driver (like mysql-connector-j). The starter provides the abstraction; the driver provides the actual database connection.

com.mysql:mysql-connector-j

Use case: connecting to a MySQL (or MySQL-compatible, like MariaDB in some cases) database. This is one of the most widely used open-source relational databases, common in web applications of every size.

pom.xml
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
spring.datasource.url=jdbc:mysql://localhost:3306/demo_db
spring.datasource.username=root
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update

org.postgresql:postgresql

Use case: connecting to a PostgreSQL database — a feature-rich, standards-compliant open-source relational database favored for complex queries, JSON columns, and strong data integrity guarantees.

pom.xml
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
spring.datasource.url=jdbc:postgresql://localhost:5432/demo_db
spring.datasource.username=postgres
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update

com.h2database:h2

Use case: an in-memory (or file-based) database, most commonly used for automated tests and local development, where you want a real relational database without installing or managing an external server. Data disappears when the application stops (unless configured to persist to a file).

pom.xml
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
application-test.properties
spring.datasource.url=jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1
spring.datasource.driver-class-name=org.h2.Driver
spring.h2.console.enabled=true
spring.jpa.hibernate.ddl-auto=create-drop
H2 Console

Click Run to see what this code prints.

com.microsoft.sqlserver:mssql-jdbc

Use case: connecting to Microsoft SQL Server, common in enterprises with a Microsoft-centric stack (Windows Server, .NET services, Azure SQL Database).

pom.xml
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
application.properties
spring.datasource.url=jdbc:sqlserver://localhost:1433;databaseName=demo_db;encrypt=true;trustServerCertificate=true
spring.datasource.username=sa
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update

Driver Comparison

DriverDatabaseTypical Use Case
mysql-connector-jMySQL / MariaDBGeneral-purpose web application databases
postgresqlPostgreSQLComplex queries, JSON data, strong standards compliance
h2H2 (in-memory or file)Automated tests and lightweight local development
mssql-jdbcMicrosoft SQL ServerEnterprise, Microsoft/Azure-centric environments

Common Mistakes

Avoid These Mistakes
  • Forgetting to add a database driver at all, then being confused by a "Failed to configure a DataSource" startup error — the persistence starter alone is not enough.
  • Leaving H2 as a compile-scope dependency in a production build instead of `runtime`, or worse, using it as an actual production database.
  • Mismatching the JDBC URL scheme (jdbc:mysql, jdbc:postgresql, jdbc:sqlserver) with the driver you actually added.

Best Practices

  • Scope database drivers as `runtime` — your code depends on the java.sql API, not on driver-specific classes directly.
  • Use H2 for fast, isolated tests, but always test against the real target database (MySQL, PostgreSQL, SQL Server) before shipping to production.
  • Keep exact driver versions aligned with what your actual database server supports, especially after major database version upgrades.

Frequently Asked Questions

Yes — a common pattern is H2 for tests and MySQL or PostgreSQL for production, selected per Spring profile via different application-{profile}.properties files.

Oracle renamed the artifact starting with the 8.1 release line; older tutorials and projects may still reference the legacy mysql-connector-java artifact ID.

Usually no — Spring Boot can infer the driver class from the JDBC URL scheme automatically. H2 is one of the few cases where explicitly setting it is still common practice.

Key Takeaways

  • JDBC drivers are separate dependencies from persistence starters and must be added alongside them, not instead of them.
  • mysql-connector-j, postgresql, and mssql-jdbc connect to their respective production databases; h2 is primarily for tests and local development.
  • Each driver pairs with a specific spring.datasource.url scheme (jdbc:mysql://, jdbc:postgresql://, jdbc:sqlserver://, jdbc:h2:mem:).

Summary

With a persistence starter and the right driver dependency together, your Spring Boot application can talk to virtually any major relational database with only a few lines of configuration. Next, the course moves beyond SQL entirely, into NoSQL databases and caching dependencies.

Next Lesson →

NoSQL & Caching Dependencies