为什么 JVM 找不到 Postgresql JDBC 驱动程序?

Why Can't JVM Find Postgresql JDBC Driver?

我知道以前有人问过这个问题,但现有的答案没有帮助。

现有答案要么告诉您将 postgresql-.jar 放入 /path/to/db/lib/,我这样做了;要么或以 MySQL 为中心。我尝试了 > 5 个不同的答案,但没有任何帮助。

这就是我在 Clojure REPL 中的 运行:

(require '[next.jdbc :as jdbc])
(def db {:dbtype "postgres" :dbname "whatever" :user "whoever" :password "yeahright"})
(jdbc/get-connection db)

我得到这个错误:

Execution error (SQLException) at java.sql.DriverManager/getConnection (DriverManager.java:702).
 No suitable driver found for jdbc:postgresql://127.0.0.1:5432/<db_name>

我知道 postgresql-42.2.12.jar 在我的计算机上的位置,我使用 :classpath-add 将该路径添加到 project.clj 中。我仔细检查了 URL 的语法,据我所知它是正确的。

Java版本:

  $ java -version
  java version "10.0.1" 2018-04-17
  Java(TM) SE Runtime Environment 18.3 (build 10.0.1+10)
  Java HotSpot(TM) 64-Bit Server VM 18.3 (build 10.0.1+10, mixed mode)

  $ lein -version
  Leiningen 2.9.1 on Java 10.0.1 Java HotSpot(TM) 64-Bit Server VM

我在 Ubuntu 和 Mac 上遇到了同样的问题。有谁知道是怎么回事吗?

确保您的 project.clj

中包含这样的条目
[org.postgresql/postgresql "42.2.12"]

:dependencies 向量中。

根据next.jdbc入门指南https://cljdoc.org/d/seancorfield/next.jdbc/CURRENT/doc/getting-started

"In addition, you will need to add dependencies for the JDBC drivers you wish to use for whatever databases you are using."

对于 lein 这意味着将以下内容添加到 project.clj 中的 :dependencies 向量中:

[org.postgresql/postgresql "42.2.10"]

(这是 next.jdbc 在撰写本文时所测试的最新版本,2020 年 5 月 23 日)