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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24504655/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 12:38:45  来源:igfitidea点击:

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

javaeclipsepostgresqljdbc

提问by rrk

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. Now my question is, how to place the file on the classpath?.

建议将 PostgreSQL 驱动程序 jar 文件放在类路径上。现在我的问题是,如何将文件放在类路径上?。

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

我是 eclipse 的新手,所以最好有详细的解释。

采纳答案by ragavi ravikumar

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 文件夹下。

回答by MSR

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

您将需要程序类路径中的 postgresql 驱动程序 .jar 文件。并检查网址是否正确是以下示例

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

回答by SparkOn

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

java.sql.SQLException: No suitable driver found是当您的连接字符串出现问题时。确保

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

are perfectly correct

完全正确

回答by Manish Kr

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!

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