简体   繁体   中英

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

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.

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.

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();
       }

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

如果您是 eclipse 中的简单动态 web projetc,则转到 web-inf 并在 lib 文件夹中粘贴 postgre jar。如果您使用的是 maven,只需使用依赖项!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM