【问题标题】:How to place PostgreSQL driver jar file on class path in eclipse?如何将 PostgreSQL 驱动程序 jar 文件放在 eclipse 中的类路径上?
【发布时间】:2014-08-21 16:22:28
【问题描述】:

在尝试使用 Eclipse 连接到 PostgreSQL 数据库的 JDBC 程序时,它标记了一个错误提示

java.sql.SQLException: No suitable driver found

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

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

【问题讨论】:

标签: java eclipse postgresql jdbc


【解决方案1】:

您需要在程序的类路径中包含 postgresql 驱动程序 .jar 文件。并检查 url 是否正确是下面的例子

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

【讨论】:

  • 如果驱动程序不在类路径中,则会出现不同的异常(即 ClassNotFoundException not SQLException
  • 我的意思是问题不是由类路径问题引起的。这是 JDBC URL 的语法问题。
  • 是的,你绝对正确。他可能在 String URL = "jdbc:postgresql://localhost:5432/your DataBase Name";
【解决方案2】:

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

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

完全正确

【讨论】:

    【解决方案3】:

    我希望你需要 buidpath 的方式。

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

    如果这对您没有帮助,请尝试手动将其复制粘贴到所需的文件夹下,希望在 lib 文件夹下。

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        【解决方案5】:

        对我有用的是: 右键单击项目->打开模块设置(F4)->单击右侧的小加号-> Jar或目录->选择驱动程序jar文件的路径->单击新条目旁边的检查表 - >确保范围设置为编译。 这对我有用,希望对其他人也有帮助。

        【讨论】:

          猜你喜欢
          • 2019-11-17
          • 1970-01-01
          • 1970-01-01
          • 2016-05-24
          • 2011-01-30
          • 1970-01-01
          • 1970-01-01
          • 2012-07-03
          • 2012-07-17
          相关资源
          最近更新 更多