How to place PostgreSQL driver jar file on class path in eclipse?

14,206

Solution 1

I hope u are in need of the way to buidpath.

Right click on the lib folder and select buildpath option >import jars>  ok  

If this doesnt help u then try copy pasting it mannually under the desired folder hopefully under lib folder.

Solution 2

You'll need the postgresql driver .jar file in the classpath of your program. and check the url is correct is the below example

 try{

            Class.forName("org.postgresql.Driver"); 

       }

       catch(ClassNotFoundException e)
       {
          system.out.println("error class not found exception");
          e.printStackTrace();

       }

       try{
           String URL = "jdbc:postgresql://localhost:5432/your DataBase Name";
           String USER = "postgres";
           String PASS = "postgres";
           Connection conn = DriverManager.getConnection(URL, USER, PASS);
           Statement st = conn.createStatement();
           ResultSet rs = st.executeQuery("Select * from employee");
           while(rs.next()){
               System.out.println(rs.getString(1));
           }

       }

       catch(Exception es){
           es.printStackTrace();
       }
Share:
14,206
rrk
Author by

rrk

Updated on June 14, 2022

Comments

  • rrk
    rrk almost 2 years

    While trying out JDBC program to connect to PostgreSQL database using eclipse it flagged an error saying

    java.sql.SQLException: No suitable driver found
    

    It was suggested to place the PostgreSQL driver jar file on the class path. Now my question is, how to place the file on the classpath?.

    I am new to eclipse so it would be better for a detailed explanation.

  • a_horse_with_no_name
    a_horse_with_no_name almost 10 years
    If the driver wasn't in the classpath there would be a different exception (namely a ClassNotFoundException not a SQLException)
  • a_horse_with_no_name
    a_horse_with_no_name almost 10 years
    My point is that the problem is not caused by a classpath issue. It's an issue with the syntax of the JDBC URL.
  • MSR
    MSR almost 10 years
    yes u r absolutely correct . he may be making mistake in String URL = "jdbc:postgresql://localhost:5432/your DataBase Name";
  • Anoop V
    Anoop V about 2 years
    Copying the Postgres JAR file to WEB-INF > lib folder solved the issue.