Spring Boot项目报错求助:Driver org.h2.Driver声明不接受jdbcUrl异常
Fix: Driver org.h2.Driver claims to not accept jdbcUrl (PostgreSQL)
Hey there, let's break down what's going wrong here and fix it step by step!
What's causing the error?
The error message spells it out clearly: you're trying to connect to a PostgreSQL database using the H2 database driver—these two don't play well together. Here are the specific issues:
- Your
application.propertiesexplicitly setsspring.datasource.driverClassName=org.h2.Driver, but your JDBC URL is for PostgreSQL (jdbc:postgresql://localhost:5432/testdb). - You have both H2 and PostgreSQL dependencies in your
pom.xml, with H2 set tocompilescope. This means it loads alongside PostgreSQL and creates a conflict.
Step 1: Fix the driver class in your config
Update your application.properties to use the correct PostgreSQL driver. You have two options:
- Set the right driver class directly:
spring.datasource.driverClassName=org.postgresql.Driver - Let Spring Boot auto-detect it: Spring Boot can automatically pick the correct driver based on your JDBC URL, so you can just delete the
spring.datasource.driverClassNameline entirely.
Step 2: Clean up your dependencies
Since you're targeting PostgreSQL, adjust your H2 dependency based on your needs:
- If you don't need H2 at all: Remove the H2 dependency block from
pom.xml:<!-- Delete this section --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> <scope>compile</scope> </dependency> - If you need H2 for testing only: Change its scope to
testso it doesn't interfere with your production PostgreSQL setup:<dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>1.4.200</version> <scope>test</scope> <!-- Update to test scope --> </dependency>
Step 3: Verify your setup
Double-check these details before restarting your app:
- Your PostgreSQL server is running on
localhost:5432 - The
testdbdatabase exists in your PostgreSQL instance - Your
spring.datasource.usernameandspring.datasource.passwordmatch your PostgreSQL credentials
After making these changes, restart your Spring Boot app—it should connect to PostgreSQL without any issues!
内容的提问来源于stack exchange,提问作者lorem77
相关产品推荐
相关产品推荐

