Spark:java.sql.SQLException:找不到适用于 jdbc 的驱动程序:postgresql://localhost/postgres

Dig*_*han 1 postgresql jdbc apache-spark

在我的 Spark 应用程序中,我尝试使用以下行连接到本地 Postgres 数据库:

val conn = DriverManager.getConnection("jdbc:postgresql://localhost/postgres", "postgres", "*Qwerty#")
Run Code Online (Sandbox Code Playgroud)

Postgres 服务器在端口 5432(默认)上运行。我也尝试过包括端口。我也尝试过Class.forName("org.postgresql.Driver"),但它抛出 ClassNotFoundException。我已确保驱动程序位于 ClassPath 中。

我正在本地模式下运行 Spark。

但我得到了上述例外。

我已经通过此处提到的方式包含了 jdbc 驱动程序sbt: https: //mvnrepository.com/artifact/org.postgresql/postgresql/42.2.2

Jac*_*ack 5

您还可以尝试以下代码:

属性 dbProperties = new Properties();

dbProperties.put("driver", "org.postgresql.Driver");

dbProperties.put("用户", "postgres");

dbProperties.put("密码", "*Qwerty#");

val conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/postgresDB",dbProperties);