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?
– Krismorte
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.
– Gustavo Fragoso
@Krismorte, I’m using Netbeans, not following Maven.
– Matheus Barbosa
@Gustavofragoso withdrew the jre6 and 7 and continued giving the same error
– Matheus Barbosa
You need to postpone the postgresql jar to your classpath
– Krismorte