You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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.properties explicitly sets spring.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 to compile scope. 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.driverClassName line 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 test so 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 testdb database exists in your PostgreSQL instance
  • Your spring.datasource.username and spring.datasource.password match your PostgreSQL credentials

After making these changes, restart your Spring Boot app—it should connect to PostgreSQL without any issues!

内容的提问来源于stack exchange,提问作者lorem77

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.30 13:09:05