0

So I've created a database in MySQL (version 8.0) and am working on an Eclispe project that should manipulate that database. However, after clicking the submit button via the Tomcat browser I run into a HTTP 500 Internal Error

"com.mysql.jdbc.Driver" 
    "javax.servlet.ServletException: com.mysql.jdbc.Driver
    com.sw.login.doPost(login.java:36)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:652)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)".

I have installed the mysql connector jar into the project's classpath repeatedly and even tried different versions of the jar file but can not get it to work. I am using a Macbook, Eclispe IDE, and MySQL. Below is a snapshot of the code for the class that should connect to my database. Any help is greatly appreciated and feel free to let me know if I need to clarify anything. Thank you.

public class UserService {

static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";  
   static final String DB_URL = "jdbc:mysql://localhost:3306/phase2";

   //  Database credentials
   static final String USER = "scott";
   static final String PASS = "apple";
   
  public static User retrieveUser(String username, String password) throws SQLException, ClassNotFoundException {
      
      Class.forName("com.mysql.jdbc.Driver");

      //STEP 3: Open a connection
      System.out.println("Connecting to database...");
     Connection conn = DriverManager.getConnection(DB_URL,USER,PASS);

      //STEP 4: Execute a query
      System.out.println("Creating statement...");
     Statement stmt = conn.createStatement();
      String sql;
      sql = "select * from user where name = \""+ username + "\" AND password = \"" + password + "\"";
      ResultSet rs = stmt.executeQuery(sql);
      
      if (rs.next()) {
          User user = new User();
          user.setName(username);
          user.setPassword(password);
          return user;    
      } else {
          return null;

      }
      
      
  };
  • Make sure to put the jar in the `lib` folder of your project. Alternatively, you can put it inside the `lib` folder to tomcat. – Arvind Kumar Avinash Mar 25 '21 at 04:06
  • `WEB-INF/lib` is where it needs to be for it to automatically be used **and deployed** on the server. – nitind Mar 25 '21 at 04:32
  • @nitind Thank you so much. By adding the mysql-connector jar to the Web-INF lib folder it seemed to finally connect to my DB. Appreciate your help! Do you have any idea why it wasn't working previously ? – DubiousDeveloper Mar 25 '21 at 14:32
  • Without a full server-side stack trace expanding on the cause for the ServletException, my guess is that you added it as an external jar and didn't enable its deployment using the project's Deployment Assembly property page. There might have been a warning in the Problems View about that, but it's easy to miss. Problem is that it's not *always* wrong to not deploy something on the Java Build Path. – nitind Mar 25 '21 at 14:35

0 Answers0