Windows 上的 docker postgres 容器未找到合适的 JDBC 驱动程序

No suitable JDBC driver found for docker postgres container on Windows

我正在尝试使用 JDBC 连接到 Postgres。 数据库 运行ning 在 Docker 容器中。 我正在使用 Windows Docker Desktop (Windows 10 pro) 和 WSL2。 我使用 docker-compose 启动 Docker 容器。 这是 docker-compose 的内容:

version: "3.8"
services:
  db:
    image: postgres:13.2
    environment:
      POSTGRES_PASSWORD: postgres
      POSTGRES_USER: postgres
      POSTGRES_DB: yoi
    ports:
      - "5432:5432"
    volumes:
      - "./postgres:/var/lib/postgresql/data"
  web:
    image: openjdk:15
    depends_on:
      - db
    ports:
      - "4567:4567"
    volumes:
      - ".:/share/yoi"
    working_dir: /share/yoi
    command: ./gradlew run

如您所见,它应该首先 运行 数据库,然后是 运行 java 应用程序。 这是 java 申请的内容:

package app;

import java.sql.DriverManager;
import java.sql.SQLException;

public final class Main {

    public static void main(final String... args) {
        System.out.println("START");
        try {
            DriverManager.getConnection(
                "jdbc:postgresql://db:5432/yoi",
                "postgres",
                "postgres"
            );
        } catch (final SQLException ex) {
            ex.printStackTrace();
        }
        System.out.println("END");
    }

}

我 运行 命令 docker-compose up 得到了这个结果:

$ docker-compose up
Creating network "javatest_default" with the default driver
Creating javatest_db_1 ... done
Creating javatest_web_1 ... done
Attaching to javatest_db_1, javatest_web_1
web_1  | Downloading https://services.gradle.org/distributions/gradle-6.7-bin.zip
db_1   |
db_1   | PostgreSQL Database directory appears to contain a database; Skipping initialization
db_1   |
db_1   | 2021-03-29 06:09:21.766 UTC [1] LOG:  starting PostgreSQL 13.2 (Debian 13.2-1.pgdg100+1) on x86_64-pc-linux-gnu, compiled by gcc (Debian 8.3.0-6) 8.3.0, 64-bit
db_1   | 2021-03-29 06:09:21.766 UTC [1] LOG:  listening on IPv4 address "0.0.0.0", port 5432
db_1   | 2021-03-29 06:09:21.766 UTC [1] LOG:  listening on IPv6 address "::", port 5432
db_1   | 2021-03-29 06:09:21.776 UTC [1] LOG:  listening on Unix socket "/var/run/postgresql/.s.PGSQL.5432"
db_1   | 2021-03-29 06:09:21.809 UTC [27] LOG:  database system was shut down at 2021-03-29 06:08:02 UTC
db_1   | 2021-03-29 06:09:21.835 UTC [1] LOG:  database system is ready to accept connections
web_1  | .........10%..........20%..........30%..........40%..........50%.........60%..........70%..........80%..........90%..........100%
web_1  |
web_1  | Welcome to Gradle 6.7!
web_1  |
web_1  | Here are the highlights of this release:
web_1  |  - File system watching is ready for production use
web_1  |  - Declare the version of Java your build requires
web_1  |  - Java 15 support
web_1  |
web_1  | For more details see https://docs.gradle.org/6.7/release-notes.html
web_1  |
web_1  | Starting a Gradle Daemon (subsequent builds will be faster)
web_1  | > Task :compileJava
web_1  | > Task :processResources UP-TO-DATE
web_1  | > Task :classes
web_1  |
web_1  | > Task :run
web_1  | START
web_1  | java.sql.SQLException: No suitable driver found for jdbc:postgresql://db:5432/yoi
web_1  |        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
web_1  |        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
web_1  |        at app.Main.main(Main.java:11)
web_1  | END
web_1  |
web_1  | Deprecated Gradle features were used in this build, making it incompatible with Gradle 7.0.
web_1  | Use '--warning-mode all' to show the individual deprecation warnings.
web_1  | See https://docs.gradle.org/6.7/userguide/command_line_interface.html#sec:command_line_warnings
web_1  |
web_1  | BUILD SUCCESSFUL in 27s
web_1  | 3 actionable tasks: 2 executed, 1 up-to-date
javatest_web_1 exited with code 0

我也试了jdbc:postgresql://localhost:5432/yoi,结果还是一样

有人知道原因和解决方法吗?

您需要将 jdbc 驱动程序添加到您的 java 应用程序。如果你有一个 Maven 项目,你可以通过向你添加以下依赖项来添加它 pom.xml:

<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.19</version>
</dependency>

对于Gradle:

dependencies {
    /* Other Dependencies... */
    implementation group: 'org.postgresql', name: 'postgresql', version: '42.2.19'
}

你能试试吗Class.forName("org.postgresql.Driver");