1. In this video I have shown how you can solve the error.
1 2 3 4 5 6 7 8 9 |
java.lang.ClassNotFoundException: org.postgresql.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 com.jinujawad.com.ConnectDB.get_connection(ConnectDB.java:23) at com.jinujawad.com.ConnectDB.main(ConnectDB.java:10) |
2. This error came for me on running the below java program in Eclipse IDE.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
package com.jinujawad.com; import java.sql.Connection; import java.sql.DriverManager; public class ConnectDB { public static void main(String[] args) { ConnectDB obj_ConnectDB=new ConnectDB(); obj_ConnectDB.get_connection(); } public Connection get_connection() { Connection connection = null; String host="localhost"; String port="5432"; String db_name="jinujawad"; String username="postgres"; String password="root"; try { Class.forName("org.postgresql.Driver"); connection = DriverManager.getConnection("jdbc:postgresql://"+host+":"+port+"/"+db_name+"", ""+username+"", ""+password+""); if (connection != null) { System.out.println("Connection OK"); } else { System.out.println("Connection Failed"); } } catch (Exception e) { e.printStackTrace(); } return connection; } } |
3. First download the postgres jar file from the official postgres site.
You can follow this link to download the jar.
https://jdbc.postgresql.org/download.html
4. Now add the jar file in Build path of the project.
Right click on the project > Build Path > Configure Build Path,
Then select libraries and add the jar to build path.