英文:
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
时,我正在链接库,因此不确定我出了什么问题。我的javac
和java
版本都是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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论