找不到合适的驱动程序(Postgres JDBC):java.sql.SQLException:找不到适用于jdbc:postgresql:// rds-service:5432 / postgres的驱动程序

时间:2020-09-21 21:11:33

标签: java postgresql jdbc centos7

我试图与CentOS 7 ec2实例上的postgres数据库建立简单连接。这是我的PostgresqlConnect.java文件:

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class PostgresqlConnect {
    
    private final static String url = "jdbc:postgresql://rds-service:5432/postgres";
    private final static String user = "root";
    private final static String password = "test123";
    
    public static void main(String[] args) {
        try{
            Connection connection = DriverManager.getConnection(url, user, password);
            if (connection!= null) {
                System.out.println("Connected successfully!");
            }
            else {
                System.out.println("Failed to connect.");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

我的运行/编译步骤如下:

> javac -cp /usr/share/java/postgresql-jdbc.jar PostgresqlConnect.java`

> java PostgresqlConnect

我明白了:

-------- PostgreSQL JDBC Connection Testing ------------
java.sql.SQLException: No suitable driver found for jdbc:postgresql://rds-service:5432/postgres
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:702)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:228)
    at PostgresqlConnect.main(PostgresqlConnect.java:15)

我这样安装了postgresql-jdbc rpm软件包:yum install postgresql-jdbc。我想在将-cp /usr/share/java/postgresql-jdbc.jar添加到我的javac -cp /usr/share/java/postgresql-jdbc.jar PostgresqlConnect.java命令时正在链接库,所以不确定我要去哪里。我的javacjava版本都是14.0.2

1 个答案:

答案 0 :(得分:1)

运行它并查看“已成功连接!”的正确方法。会是

> javac PostgresqlConnect.java
> java -cp /usr/share/java/postgresql-jdbc3.jar PostgresqlConnect.java