简体   繁体   English

如何将 PostgreSQL 驱动程序 jar 文件放在 eclipse 的类路径上?

[英]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在尝试使用 Eclipse 连接到 PostgreSQL 数据库的 JDBC 程序时,它标记了一个错误说

java.sql.SQLException: No suitable driver found

It was suggested to place the PostgreSQL driver jar file on the class path.建议将 PostgreSQL 驱动程序 jar 文件放在类路径上。 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.我是 eclipse 的新手,所以最好有详细的解释。

I hope u are in need of the way to buidpath.我希望你需要通往 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.如果这对您没有帮助,请尝试手动将其复制粘贴到所需的文件夹下,希望在 lib 文件夹下。

You'll need the postgresql driver .jar file in the classpath of your program.您将需要程序类路径中的 postgresql 驱动程序 .jar 文件。 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. java.sql.SQLException: No suitable driver found是当您的连接字符串有问题时。 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.对我有用的是:右键单击项目-> 打开模块设置(F4)-> 单击右侧的小加号-> Jar 或目录-> 选择驱动程序jar 文件的路径-> 单击检查在表中的新条目旁边 -> 确保范围设置为编译。 This worked out for me, hope it helps someone else too.这对我有用,希望它也能帮助其他人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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