try { DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); StringBuilder url = new StringBuilder(); url. append("jdbc:postgresql:"). append("table"); Connection connection = DriverManager.getConnection(url.toString(), "postgres", "asdasdasd12"); return connection; } catch (SQLException | InstantiationException | IllegalAccessException | ClassNotFoundException e) { e.printStackTrace(); } 

I get an exception in the string

 DriverManager.registerDriver((Driver) Class.forName("org.postgresql.Driver").newInstance()); 

java.lang.ClassNotFoundException: org.postgresql.Driver at java.net.URLClassLoader $ 1.run (URLClassLoader.java=59) at java.net.URLClassLoader $ 1.run (URLClassLoader.java.0248) at java.security.AccessControl. doPrivileged (Native Method) at java.net.URLClassLoader.findClass (URLClassLoader.java.7347) at java.lang.ClassLoader.loadClass (ClassLoader.java:423) at sun.misc.Launcher $ AppClassLoader.loadClass (Launcher.java: 308) at java.lang.ClassLoader.loadClass (ClassLoader.java天56) at java.lang.Class.forName0 (Native Method) at java.lang.Class.forName (Class.java:258) at ua.kiev.makson .database.DBService.getPostgresqlConnection (DBService.java:36) at ua.kiev.makson.database.DBService. (DBService.java:12) at ua.kiev.makson.Testing.main (Testing.java:14) at sun .reflect.NativeMethodAccessorImpl.invoke0 (Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:57) at sun.reflect.DelegatingMethodAccessorImpl.invoke (in the one of our code is our application, in the one of your code is our application, in the one of your affiliate code of your company. Method.invoke (Method.java:491) at com.intellij.rt.execution.application.AppMain.main (AppMain.java:140)

my build.gradle :

 group 'ua.kiev.makson' version '1.0-SNAPSHOT' apply plugin: 'java' sourceCompatibility = 1.8 repositories { mavenCentral() } dependencies { compile 'log4j:log4j:1.2.17' compile 'org.slf4j:slf4j-api:1.7.5' compile 'org.slf4j:slf4j-log4j12:1.7.5' compile 'postgresql:postgresql:9.1-901-1.jdbc4' compile group: 'junit', name: 'junit', version: '4.4' } 

jar gradle

Tell me what to fix.

  • jdbc driver version 4 and higher is not necessary to register in the DriverManager. try without this line DriverManager.registerDriver ((Driver) Class.forName ("org.postgresql.Driver"). newInstance ()); - Artem Konovalov
  • @ArtemKonovalov java.sql.SQLException: No suitable driver found for jdbc: postgresql: table - still throws an error - makson
  • How do you build / run a project? Through the grad? - Alexandr
  • one
    Idea 2016.2.2 - it all worked with your build.gradle. The only thing that Idea didn’t like is ubuntu’s hail, because There were no rights to write, put the grad 3.0 in a separate folder. And I did not create in Idea, but I created it with pens, and then I opened it in Idea. - Russtam
  • one
    @ArtemKonovalov re-created the project and everything works fine. most likely when creating a project for the first time something messed up, since the first time I created a gradle project - makson

0