【问题标题】:Can't connect to postgresql with java [closed]无法使用 java 连接到 postgresql [关闭]
【发布时间】:2021-03-07 18:19:43
【问题描述】:

在这个简单的示例中,我无法连接到 postgresql 表。而且我不知道出了什么问题。请帮忙。

package postgresql.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;



public class PostgresWithJDBCConnection {
    public static void main(String[] args) {
         // establishes database connection
        try (Connection connection =  DriverManager.getConnection("jdbc:postgresql://127.0.0.1:5432/students", "postgres", "postgres")) {
            System.out.println("Connection established successfully");
        } catch (SQLException e) {
            System.out.print(e.getMessage());
        }
    }
}

控制台返回

“没有找到适合 jdbc:postgresql://127.0.0.1:5432/students 的驱动”

【问题讨论】:

  • 您是否下载了 Postgres 的 JDBC 驱动程序?如果是这样,是哪一个,哪个版本?你把司机的 JAR 放在哪里了?您的部署方案是什么?桌面应用?网络应用?如果是 web 应用程序,什么 web 容器?你是从 IDE 运行的吗?由于缺乏调试细节而投票结束。
  • 请说明您是如何运行程序的。该错误表明 PostgreSQL JDBC 驱动程序不在类路径中。

标签: java postgresql jdbc


【解决方案1】:

下载 postgresql jdbc (https://jdbc.postgresql.org/) 并将其添加到您的类路径中

【讨论】:

    猜你喜欢
    • 2016-06-07
    • 2014-10-19
    • 2016-07-13
    • 1970-01-01
    • 2012-11-01
    • 2019-11-02
    • 1970-01-01
    • 2011-03-29
    • 1970-01-01
    相关资源
    最近更新 更多