0

thanks for any help, I am a complete noob here but trying to learn. the below code is simply trying to create a connection to a database. I am getting this error back:

java.lang.ClassNotFoundException: org.postgres.Driver
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at CreateDB.main(CreateDB.java:11)
java.lang.ClassNotFoundException: org.postgres.Driver

having researched online the solution i come across is to check the library is added to to build path. i can confirm that i have (I think). to do this i right clicked the prject -> Properties -> Java Build Path -> external JARS and navigated to the postgresql - 42.41.4..jar which is located in "...\eclipse-workspace\libraries\" I can see that the library is added under referenced libraries within the project. not a clue now im stuck. any help is genuinely appreciated.

I am learning from here https://www.tutorialspoint.com/postgresql/postgresql_java.htm

import java.sql.Connection;
import java.sql.DriverManager;

public class CreateDB {
    public static void main(String Args[]) {
        Connection c = null;
        try {
            Class.forName("org.postgres.Driver");
            c = DriverManager.getConnection("jdbc.postgresql://localhost:1080/VEM", "postgres", "Diablo12" ); //creates connection with U&P
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() +": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Database opened successfully");
    }
}
1
  • Apart from the wrong class name (see Henry's answer): the Class.forName() is no longer needed since Java 6 Commented Aug 24, 2017 at 13:13

1 Answer 1

1

The correct driver name is: org.postgresql.Driver. So your code should look like this:

import java.sql.Connection;
import java.sql.DriverManager;

public class CreateDB {
    public static void main(String args[]) {
        Connection c = null;
        try {
            Class.forName("org.postgresql.Driver");
            c = DriverManager.getConnection("jdbc.postgresql://localhost:1080/VEM", "postgres", "Diablo12" ); //creates connection with U&P
        } catch (Exception e) {
            e.printStackTrace();
            System.err.println(e.getClass().getName() +": " + e.getMessage());
            System.exit(0);
        }
        System.out.println("Database opened successfully");
   }
}

The false driver name causes java to throw the ClassNotFoundException because the class with the passed name could not be found in the classpath. Since Java 6 the loading of the driver via Class.forName() is not needed any more, like a_horse_with_no_name pointed out in his comment.

Sign up to request clarification or add additional context in comments.

2 Comments

Or simply leave out the Class.forName() this is not needed any more
@a_horse_with_no_name That's true. I explained it because it was the reason of the failure.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.