0

I am currently testing my database connection:

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.DriverManager;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.*;

@WebServlet("/servlet/casillano.testdb.TestDbServlet")
public class TestDbServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        String user="";
        String pass="";

        String url="jdbc:mysql://localhost:3306/birthday-wishlist-db?useSSL=false&serverTimezone=UTC";
        String driver = "com.mysql.cj.jdbc.Driver";

        try {
            PrintWriter out = response.getWriter();
            out.println("Connection to databse: " + url);
            Class.forName(driver);
            Connection conn = DriverManager.getConnection(url, user, pass);
            out.println("Connection successful");
            conn.close();
        } catch (Exception e) {
            e.printStackTrace();
            throw new ServletException(e);
        }

    }
}

It was successful at first, but after a while, a red x popped up on my project and nowhere else. When I tried running this code again, I got a ClassNotFoundException.

My pom.xml contains the mysql dependency:

        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.18</version>
        </dependency>

So I'm not sure what's wrong. I tried cleaning and doing a Maven update on the project, but nothing worked.

  • 3
    The call to `Class.forName(...)` should be not required at this point. Try removing it. Are you using an IDE such as IntelliJ, or are you using the command line? – Tim Biegeleisen Jun 14 '20 at 02:45
  • What IDE are you using? – Femi Jun 14 '20 at 03:21
  • @TimBiegeleisen I'm using Eclipse Java EE. I removed the call and I'm getting a `java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:3306/birthday-wishlist-db?useSSL=false&serverTimezone=UTC with root cause`. – counterhash Jun 14 '20 at 04:07
  • @Femi Eclipse Java EE. Using a local tomcat server if that helps. – counterhash Jun 14 '20 at 04:08
  • 1
    The problem is that the MySQL JAR is not on your Eclipse classpath. I don't have an exact solution for you, but this is the problem. – Tim Biegeleisen Jun 14 '20 at 04:08
  • @TimBiegeleisen I checked the Java Build Path for my project and it shows `mysql-connector-java-8.0.18.jar` on the build path. I'm still pretty new to this, so does that mean that the jar is in the classpath? – counterhash Jun 14 '20 at 04:17
  • It is there when compiling but it it there when running? Does it get packaged with the WAR or EAR file? – user207421 Jun 14 '20 at 04:38
  • I was able to fix it, although I'm not sure how the fix is related to the error above. I had a project error saying `Cannot change version of project facet Dynamic Web Module to 3.1`. I checked Project -> Properties -> Project Facets, and then unchecked Dynamic Web Module. Then I went to Maven -> Update Project, and everything works fine. – counterhash Jun 14 '20 at 04:49

0 Answers0