我正在尝试使用JDBC连接到Postgres。数据库正在Docker容器中运行。我使用的是带有WSL2的Windows Docker Desktop(Windows 10 pro(。我使用Docker compose启动Docker容器。以下是docker撰写的内容:
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中添加以下depencency来添加它:
<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");
吗