我的代码访问本地数据库时遇到问题。我正在使用 Eclipse 和 postgresql。
我很清楚已经有几篇这样的帖子,但它们没有帮助我解决我的问题,所以这就是我问自己的原因。
驱动程序的 jar 文件已经在库中并且也被添加到构建路径中。但是我仍然从找不到驱动程序中得到错误。
无法获得连接:java.sql.SQLException:找不到适合 psql -h localhost beleg postgres 的驱动程序
我的代码如下所示:
package DB;
import java.sql.*;
public class Connections {
private Connection dbcon;
private Statement stmt;
private Statement stmt2;
Connections(String db_url, String username, String password) {
try {
Class.forName("org.postgresql.Driver");
dbcon = DriverManager.getConnection(db_url, username, password);
stmt = dbcon.createStatement();
stmt2 = dbcon.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
} catch (Exception e) {
System.out.println("Can't get a connection: " + e.toString());
try {
if (stmt != null)
stmt.close();
} catch (SQLException f) {
} …Run Code Online (Sandbox Code Playgroud)