0

I am new to Java and I am trying to connect to my local PSql database without success, below the code and the details of the error.

I have 2 files: DbContract.java and TestConnection.java and I am using Eclipse.

DbContract.java

package dbcontract.db;

public interface DbContract {

    public static final String HOST = "jdbc:postgresql://localhost:5432/";
    public static final String DB_NAME = "db_notespesa";
    public static final String USERNAME = "postgres";
    public static final String PASSWORD = "";
}

TestConnection.java

package dbcontract;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import dbcontract.db.DbContract;

public class TestConnection {

    public static void main(String[] args) {
        try {
            Class.forName("org.postgresql.Driver");
            Connection conn = DriverManager.getConnection(
                    DbContract.HOST+DbContract.DB_NAME,
                    DbContract.USERNAME,
                    DbContract.PASSWORD);
            System.out.println("DB connected");

        } catch (ClassNotFoundException | SQLException e) {
            e.printStackTrace();
        }
    }
}

Error:

java.lang.ClassNotFoundException: org.postgresql.Driver
    at java.base/jdk.internal.loader.BuiltinClassLoader.loadClass(BuiltinClassLoader.java:583)
    at java.base/jdk.internal.loader.ClassLoaders$AppClassLoader.loadClass(ClassLoaders.java:178)
    at java.base/java.lang.ClassLoader.loadClass(ClassLoader.java:521)
    at java.base/java.lang.Class.forName0(Native Method)
    at java.base/java.lang.Class.forName(Class.java:315)
    at TestConnection.main(TestConnection.java:13)
Marco G. de Pinto
  • 587
  • 2
  • 10
  • 27

1 Answers1

1

You need to add the dependency for the driver of Postgress as the error explains , if you are using maven just add this to pom.xml :

    <`<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
    <groupId>org.postgresql</groupId>
    <artifactId>postgresql</artifactId>
    <version>42.2.5</version>
</dependency>

Updated the version as Mark mentioned in the comment , here is the link for all the versions on maven repo : https://mvnrepository.com/artifact/org.postgresql/postgresql

Mohammad Karmi
  • 1,219
  • 1
  • 21
  • 37
  • I am not using Maven, I have installed psql from the following link: https://www.postgresql.org/download/macosx/ . Inserting the file as you described unfortunately does not work.. – Marco G. de Pinto Dec 14 '18 at 16:29
  • 1
    Version `postgresql:postgresql:9.1-901-1.jdbc4` is extremely old. The latest version is `org.postgresql:postgresql:42.2.5`, see https://search.maven.org/search?q=g:org.postgresql%20AND%20a:postgresql – Mark Rotteveel Dec 14 '18 at 16:29
  • 1
    @MarcoG.dePinto Just downloading a JDBC driver is not enough, you'll need to add it to the runtime classpath of your application. – Mark Rotteveel Dec 14 '18 at 16:29
  • @MarkRotteveel: How can I add it using Eclipse? – Marco G. de Pinto Dec 14 '18 at 16:41
  • 1
    @MarcoG.dePinto create maven project and then add to pom.xml http://www.vogella.com/tutorials/EclipseMaven/article.html – Mohammad Karmi Dec 14 '18 at 16:42
  • 1
    @MarcoG.dePinto I strongly suggest to start using Maven or Gradle (Eclipse supports them), but otherwise look at [How to import a jar in Eclipse](https://stackoverflow.com/questions/3280353/how-to-import-a-jar-in-eclipse) – Mark Rotteveel Dec 14 '18 at 16:43
  • Thank you, marked the question as solved! – Marco G. de Pinto Dec 14 '18 at 16:44