I'm trying to connect to my postgresql database on my localhost with Java and JDBC. But getting error that is no suitable driver found for this. I successfully connected to the database in the IntelliJ IDEA through in the "Database" tab, but I cannot connect in the code.
This is my Main.java:
package org.example;
import java.sql.Connection;
import java.sql.DriverManager;
public class Main {
public static final String url="jdbc:postgresql://localhost:1234/testDatabase";
public static final String user="postgres";
public static final String password="1234maev";
public static void main(String[] args) {
Connection connection = null;
try {
connection = DriverManager.getConnection(url, user, password);
System.out.println("Connected to the PostgreSQL server successfully.");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
}
I've JDK version 1.8 and downloaded the .jar file with the postgresql JDBC driver version 42.6.0, included it in the libraries and in the modules (screenshots below).
How can I fix it?
I expected that my java app will connect to the database. I tried to add driver again, but it didn't work. Connection through the "Databases" tab in IntelliJ IDEA with host, port, user and password worked fine.