I know this question has been asked before but it is not a duplicate. I have included the mysql connector jarfile to the projects build path(thus the answers to this previously asked question does not help). The connection works when calling my database class using a test driver, but it does not work when using a servlet with tomcat. I am really not sure what the issue is. Here is my code for my constructor which creates the db connection for mysql:
import java.sql.*;
public class DatabaseManager {
private Connection con;
private Statement st;
private ResultSet rs;
public DatabaseManager()
{
System.out.println("Connecting to database");
try {
Class.forName("com.mysql.cj.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase","username","password");
st=con.createStatement();
}
catch(Exception e)
{
System.out.println("Cannot connect to database. Error: " + e);
return;
}
System.out.println("Connection Successful");
}
}
I call this constructor in a testdriver(using the main method entry point) and the connection works(no exceptions are thrown). However when attempting to call this same class in a servlets doPost() method(using the servlet class api), I get the error labeled above. The servlets code is simply:
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
DatabaseManager manager = new DatabaseManager();
}
Both the testdriver, the servlet and the DBManager are in the same project. I only was using a testdriver for testing purposes.