【问题标题】:java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgresjava.sql.SQLException: 找不到适合 jdbc:postgresql://localhost:5432/postgres 的驱动程序
【发布时间】:2022-09-26 13:56:17
【问题描述】:
    import java.sql.*;
    
    public class JDBCExample {
       static final String DB_URL = \"jdbc:postgresql://localhost:5432/postgres\";
       static final String USER = \"postgres\";
       static final String PASS = \"Vinr\";
       static final String QUERY = \"SELECT id, first, last, age FROM Employees\";
    
       public static void main(String[] args) {
          // Open a connection
          try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(QUERY);) {
             // Extract data from result set
             while (rs.next()) {
                // Retrieve by column name
                System.out.print(\"ID: \" + rs.getInt(\"id\"));
                System.out.print(\", Age: \" + rs.getInt(\"age\"));
                System.out.print(\", First: \" + rs.getString(\"first\"));
                System.out.println(\", Last: \" + rs.getString(\"last\"));
             }
          } catch (SQLException e) {
             e.printStackTrace();
          } 
       }
    }

得到错误为

java.sql.SQLException: No suitable driver found for jdbc:postgresql://localhost:5432/postgres
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:708)
        at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)
        at JDBCExample.main(JDBCExample.java:11)

    标签: java postgresql


    【解决方案1】:

    您在运行时错过了类路径中的postgresql driver

    Download 并将其添加到您的类路径中。

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 1970-01-01
      • 2018-11-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多