Reputation: 37
I'm building a personal website and have decided to use AWS RDS for my database and connecting my back end to it using JDBC. I have a main method I have created to test my connection to AWS RDS and it returns my tables from my database into my eclipse console properly. However, when I launch Tomcat 9 and open my website locally in Google Chrome I get the error message java.lang.ClassNotFoundException: com.mysql.cj.jdbc.Driver. It's as if my driver isn't being recognized my Tomcat. I have tried creating a copy of my Connector driver for MySQL and putting it in my Tomcat's lib folder but that didn't fix anything.
DButil class that establishes connection to my DB. my connectionURL string has been modified to hide my real url for security.
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.");
}
}
}
my DAO class.
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;
}
}
Upvotes: 1
Views: 3822
Reputation: 10734
How did you package your project? Here is an AWS tutorial that shows you how to build an example Java WEB app (this example app is developed by using Spring BOOT) that uses Amazon RDS to store application data.
In this example app, the MySQL Driver is specified in the POM file:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
Then the project is packaged by using a Maven command and deployed to Elastic Beanstalk.
Creating the Amazon Relational Database Service item tracker
Upvotes: 4