我正在建立一个个人网站,并决定将AWS RDS用于我的数据库,并使用JDBC将我的后端连接到它。我创建了一个main方法来测试我与AWS RDS的连接,它将我的表从我的数据库正确地返回到我的eclipse控制台。然而,当我启动Tomcat9并在Google Chrome本地打开我的网站时,我得到了错误消息java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver。好像我的司机没有被我的Tomcat认出。我曾尝试为MySQL创建连接器驱动程序的副本,并将其放入Tomcat文件夹中,但这并没有解决任何问题。
建立到我的数据库的连接的DButil类。出于安全考虑,我的connectionURL字符串已被修改为隐藏我的真实url。
public class DButil {
private static String connectionURL = "jdbc:mysql://demo.code.us-east-1.rds.amazonaws.com/tablename?user=admin&password=password";
public static Connection getConnection() {
Connection connection = null;
try {
Class.forName("com.mysql.cj.jdbc.Driver");
connection = DriverManager.getConnection(connectionURL);
} catch (SQLException | ClassNotFoundException e) {
e.printStackTrace();
}
return connection;
}
public static void main(String[] args) throws SQLException {
Connection conn = DButil.getConnection();
if (null != conn) {
System.out.println("A Complete Connection.");
DatabaseMetaData metaData = conn.getMetaData();
ResultSet rs = metaData.getTables(null, null, "%", null);
while (rs.next()) {
System.out.println(rs.getString(3));
}
} else {
System.out.println("Error, No Connection.");
}
}
}我的刀课。
public class AgrDao {
private String selectAllFromShows = "SELECT showId, showImage, showLocation, entryPrice, showDate, upcoming, showName FROM Shows";
public List<Show> getAllShows() {
List<Show> shows = new ArrayList<Show>();
ResultSet result = null;
Statement statement = null;
Connection conn = DButil.getConnection();
try {
statement = conn.createStatement();
result = statement.executeQuery(selectAllFromShows);
while (result.next()) {
Show show = createShow(result);
shows.add(show);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (conn != null) {
result.close();
statement.close();
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return shows;
}
private Show createShow(ResultSet result) throws SQLException {
Show newShow = new Show(result.getInt("showId"),
result.getString("showName"),
result.getString("showImage"),
result.getString("showLocation"),
result.getDouble("entryPrice"),
result.getString("showDate"),
result.getBoolean("upcoming"));
return newShow;
}
}发布于 2021-03-16 02:01:17
你是如何打包你的项目的?这是一篇AWS教程,向您展示了如何构建一个使用Amazon RDS存储应用程序数据的示例Java WEB应用程序(此示例应用程序是使用Spring BOOT开发的)。

在此示例应用程序中,MySQL驱动程序在POM文件中指定:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>然后使用Maven命令将项目打包并部署到Elastic Beanstalk。
Creating the Amazon Relational Database Service item tracker
https://stackoverflow.com/questions/66643043
复制相似问题