Java. Lang. Classnotfoundexception: Org. Postgresql. Driver Problem [Duplicate]

Java. Lang. Classnotfoundexception: Org. Postgresql. Driver Problem [Duplicate]

I've been working on a web project with Java EE and PostgreSQL and it runs perfectly. I tried moving it to another machine, but now it's not connecting to the database anymore.

I tried multiple solutions but nothing works so far:

  • adding postgresql.jar to java build path
  • moving it to the /lib file of tomcat server

Hers is my connection to database class:

public class DBConnexion {
    private static Connection con=null;


private DBConnexion(){
    try
    {
        try {
            Class.forName("org.postgresql.Driver");
            //getConnection(url:dataBase name, owner name , password) 
            con=(Connection)DriverManager.getConnection("jdbc:postgresql:septentrion", "postgres","123");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    catch(SQLException e1)
    {
        e1.printStackTrace();
    }
}

public static Connection getInstance()
{
    if(con==null)
        new DBConnexion();
    return con;
}



}
2

1 Answer

Depending on your dependency manager, you will have to add postgresql as your dependency. For example if you are using Maven then

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

If you are not using anything(which I think is the case), then you will have to manually download the jar file and put it in your classpath.

EDIT: Since you do not use any dependency manager, if you want to compile com.example.Foo that depends on lib/postgresql-42.2.6.jar you might use the following incantation:

javac -classpath lib/postgresql-42.2.6.jar com/example/Foo.java

12
James H. Sterling
Author

James H. Sterling

James Sterling reports on renewable energy developments, climate policy, ecological conservation, and green tech innovations around the globe.