I start a project with Java (8, SDK 21.0.2) on IntelliJ IDEA Community Edition 2022.1.1. MMy database is on PostgreSql (16). I created the class + method below, in order to test my database connexion :
import java.sql.*;
public class Connect {
public static void main(String[] args) throws ClassNotFoundException {
try {
Connection connection = null;
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection("jdbc:postgresql://localhost:PORT/DB", "USER", "PASSWORD");
System.out.println("OK ! :)");
connection.close();
} catch (SQLException e) {
e.printStackTrace();
System.out.println("KO ! :(");
}
}
}
I download the driver postgresql-42.7.3.jar, which I put in a repository "lib" within my project. On IntelliJ, I went in File > Project Structure > Modules > Dependencies > I added postgresql-42.7.3.jar. The way associated is exactly the same than the repository "lib" in my project. But when I write the command "java Connect.java", I got the error "Exception in thread "main" java.lang.ClassNotFoundException: org.postgresql.Driver". Have I missed something in the manual installation of the driver ? (I don't use Gradle or Maven) Thanks for your answers !
- I installed the drive as dependency of my project
- I installed the plugin DB Navigator on Intellij > I provided the informations about my database > the connexion test is successfull
- I wrote the absolute way of my driver in my environment variables