2

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.

vzamanillo
  • 9,905
  • 1
  • 36
  • 56
rrk
  • 109
  • 1
  • 3
  • 11
  • "*No suitable driver found*" usually means your JDBC URL is wrong. If the class is not available (because of a classpath issue) you'd get a `ClassNotFoundException` –  Jul 01 '14 at 07:28
  • possible duplicate of [SQLException: No suitable driver found](http://stackoverflow.com/questions/5982675/sqlexception-no-suitable-driver-found) – vzamanillo Jul 01 '14 at 07:32
  • @vzamanillo I think it's rather a duplicate of this: http://stackoverflow.com/questions/23558568/loading-the-postgresql-jdbc-4-1-driver/23559085 –  Jul 01 '14 at 07:44

5 Answers5

5

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.

1

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();
       }
MSR
  • 535
  • 7
  • 19
  • If the driver wasn't in the classpath there would be a different exception (namely a `ClassNotFoundException` **not** a `SQLException`) –  Jul 01 '14 at 07:45
  • My point is that the problem is **not** caused by a classpath issue. It's an issue with the syntax of the JDBC URL. –  Jul 01 '14 at 07:51
  • yes u r absolutely correct . he may be making mistake in String URL = "jdbc:postgresql://localhost:5432/your DataBase Name"; – MSR Jul 01 '14 at 08:10
0

java.sql.SQLException: No suitable driver found is when there is something wrong with your connection-string. Make sure

Class.forName("org.postgresql.Driver");
String url = "jdbc:postgresql://HOST/DATABASE";

are perfectly correct

SparkOn
  • 8,806
  • 4
  • 29
  • 34
0

if you are simple dynamic web projetc in eclipse then go to web-inf and in lib folder paste the postgre jar .and if you are using maven simply use the dependency!

Manish Kr
  • 51
  • 1
  • 1
0

What worked for me was: Right click on project -> Open Module Settings(F4) -> click on the small plus from the right side -> Jar or directories -> Select the path to the driver jar file -> Click on the check next to the new entry in the table -> Make sure the scope is set to compile. This worked out for me, hope it helps someone else too.