我正在进行堆叠竞赛。请注意,我的AWSdb URL是这篇文章的假URL。
java.sql.SQLException: No suitable driver found for "MyAWSDB-URL?currentSchema=mySchema"
这是我的连接工厂(JDBC(
public class ConnectionFactory {
private static ConnectionFactory cf = new ConnectionFactory(1);
public static ConnectionFactory getConnectionFactory() {
return cf;
}
private Connection[] conn;
private ConnectionFactory(int numberOfConnections) {
try {
Class.forName("org.postgresql.Driver");
} catch (ClassNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
String url = System.getenv("DB_URL");
String user = System.getenv("DB_USER");
String password = System.getenv("DB_PASS");
try {
this.conn = new Connection[numberOfConnections];
for (int i = 0; i < this.conn.length; i++) {
Connection conn = DriverManager.getConnection(url, user, password);
this.conn[i] = conn;
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public Connection getConnection() {
return this.conn[0];
};
public void releaseConnection(Connection conn) {
//do nothing - release conn after use if in multiple threads
};
这是使用我的连接工厂的DAO(数据访问对象(
public class EmployeeDAO {
private ConnectionFactory cf = ConnectionFactory.getConnectionFactory();
// -----------------------------------------------------------LOGIN AND GET EMPLOYEE CREDENTIALS--------------------------------------------------------------------------------
public User login(String username, String pass) {
Connection conn = this.cf.getConnection();
User loginCredentials = null;
try {
String sql = "select * from "user" u rn"
+ "where username = "+username+" and pass = "+pass;
Statement s = conn.createStatement();
ResultSet res = s.executeQuery(sql);
while(res.next()) {
loginCredentials = new User(res.getInt("user_id"),res.getString("fname"), res.getString("lname"), res.getString("username"), res.getString("pass"), res.getString("email"));
}
System.out.println(loginCredentials.toString());
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return loginCredentials;
}
}
这是一个专业的项目,我在pom.xml 上有我的依赖项
<!-- https://mvnrepository.com/artifact/org.postgresql/postgresql -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<version>42.2.18</version>
</dependency>
我已经正确设置了我的ENV变量。IDK我做错了什么。请领我朝正确的方向走。我的WEB-INF->lib上没有
传递给DriverManager.getConnection
的URL不是有效的JDBC URL。
Url应如下所示:
jdbc:postgresql://postgresql-instance1.cg034hpkmmjt.us-east-1.rds.amazonaws.com/myDatabase
当然,这只是一个例子。
您需要提供前缀jdbc:postgresql://
,然后提供连接字符串的其余部分。您可以在亚马逊RDS控制台的数据库详细信息页面中找到确切的值。
请考虑复习AWS文档中关于如何在Amazon RDS中连接PostgreSQL数据库的第4点。