Problems connecting Postgres with java

Asked

Viewed 295 times

0

Well, I’m having a problem connecting my java application to my database (I’m using pgAdmin4 which is basically postgres).

Java code:

public class ConnectionFactory {

private static final String DRIVER = "org.postgressql.Driver";
private static final String URL = "jdbc:postgresql://localhost:5432/NomedoDB";
private static final String USER = "postgres";
private static final String PASS = "";

public static Connection getConnection() throws SQLException {
    try {
        Class.forName(DRIVER);

        return DriverManager.getConnection(URL, USER, PASS);
    } catch (ClassNotFoundException ex) {
        throw new RuntimeException("Erro na conexão", ex);
    }
}
}

Main:

public static void main(String[] args) throws SQLException {
    // TODO code application logic here
    ConnectionFactory conn = new ConnectionFactory();
    conn.getConnection();
}

Error:

Exception in thread "main" java.lang.Runtimeexception: Connection error At Connectionfactory.getConnection(Connectionfactory.java:22) At Bdlearning.main(Bdlearning.java:22) Caused by: java.lang.Classnotfoundexception: org.postgressql.Driver at java.net.Urlclassloader.findClass(Urlclassloader.java:381) at java.lang.Classloader.loadClass(Classloader.java:424) at sun.misc.Launcher$Appclassloader.loadClass(Launcher.java:335) at java.lang.Classloader.loadClass(Classloader.java:357) at java.lang.Class.forName0(Native Method) at java.lang.Class.forName(Class.java:264) At Connectionfactory.getConnection(Connectionfactory.java:18) ... 1 more

I saw in some places that could be the driver of postgres, but I’ve tried with several and even then it didn’t work, I’m currently using these here which were lowered in https://jdbc.postgresql.org/download.html#Current

  • Through which IDE are you developing? Your project follows the Maven model?

  • I believe that only need to add a jar to your project, in case should be removed the jre6 and 7 that you put. Check this out: https://answall.com/questions/242714/por-que-utilizar-class-forname-ao-conectar-com-banco-data.

  • @Krismorte, I’m using Netbeans, not following Maven.

  • @Gustavofragoso withdrew the jre6 and 7 and continued giving the same error

  • You need to postpone the postgresql jar to your classpath

1 answer

0


It took me a while to realize your error, it is simple typing question because the correct name of the driver is Class.forName("org.postgresql.Driver") you put 2’s'.

  • I’ve noticed this before, but I think I got a Ctrl+z when I was doing the tests from upstairs. I think the fact that I took the 2 jars that were left and your help worked. Thanks !

Browser other questions tagged

You are not signed in. Login or sign up in order to post.