没有找到适合jdbc:postgresql的驱动程序,但我安装了驱动程序



我想连接到我的数据库,所以我创建了这个类来连接

public class DBConnection {
private final String url = "jdbc:postgresql://localhost:5433/Litopia";
private final String user = "postgres";
private final String password = "postgres";
public Connection connect() {
Connection conn = null;
try {
conn = DriverManager.getConnection(url, user, password);
System.out.println("[LitopiaServices] Connected to the PostgreSQL");
} catch (SQLException e) {
System.out.println(e.getMessage());
}
return conn;
}
}

因为我在我的pom中使用maven.xml所以我添加了这个:

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

但是当我像那样运行我的代码时

DBConnection db = new DBConnection();
db.connect();

我有 jdbc:postgresql 的未找到合适的驱动程序异常...但如您所见,我已经将我的驱动程序和连接类安装为与文档示例相同的代码。所以我真的不明白发生了什么。

所以我的问题来自不会被罚款的类名,所以为了解决它,我在连接之前添加了:

Class.forName("org.postgresql.Driver");
conn = DriverManager.getConnection(url, user, password);

它有效!

最新更新