【发布时间】: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