No Suitable Driver Found (Postgres JDBC) : java.sql.SQLException: No suitable driver found for jdbc:postgresql://rds-service:5432/postgres

huangapple go评论80阅读模式
英文:

No Suitable Driver Found (Postgres JDBC) : java.sql.SQLException: No suitable driver found for jdbc:postgresql://rds-service:5432/postgres

问题

以下是您的翻译内容:

我正在尝试在CentOS 7的EC2实例上与我的PostgreSQL数据库建立简单的连接。以下是我的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("连接成功!");
            }
            else {
                System.out.println("连接失败。");
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

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

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

> java PostgresqlConnect

然后我得到这个:

-------- PostgreSQL JDBC连接测试 ------------
java.sql.SQLException: 无法找到适当的驱动程序以用于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。我认为在我的javac -cp /usr/share/java/postgresql-jdbc.jar PostgresqlConnect.java命令中添加了-cp /usr/share/java/postgresql-jdbc.jar时,我正在链接库,因此不确定我出了什么问题。我的javacjava版本都是14.0.2

英文:

I am trying to make a simple connection to my postgres database on CentOS 7 ec2 instance. Here is my PostgresqlConnect.java file:

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

}

My steps to run/compile are as follows:

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

> java PostgresqlConnect

And I get this:

-------- 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)

I installed the postgresql-jdbc rpm package as such: yum install postgresql-jdbc. I think I'm linking the library when I add -cp /usr/share/java/postgresql-jdbc.jar to my javac -cp /usr/share/java/postgresql-jdbc.jar PostgresqlConnect.java command so not sure where I'm going wrong. My javac and java versions are both 14.0.2.

答案1

得分: 1

正确的运行方式并且看到 "连接成功!" 的方法是:

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

The correct way to run it and see "Connected successfully!" would be

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

huangapple
  • 本文由 发表于 2020年9月22日 05:11:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/64000045.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定