1

I'm new to JSP, so I'm playing around with it a bit. I've created a Maven project in Intellij and imported the dependencies that I need in my pom.xml, namely mysql-connector and servlet-api:

<dependencies>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
    </dependency>

    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.20</version>
        <scope>compile</scope>
    </dependency>
</dependencies>

I have a JSP file that accesses the local MySQL database using the corresponding Driver.

<%
    String url = "jdbc:mysql://localhost:80/DemoJSP";
    String username = "root";
    String password = "";

    Class.forName("com.mysql.jdbc.Driver");
    Connection connection = DriverManager.getConnection(url, username, password);
%>

However, when I run the Tomcat server, I get HTTP Status 500. The cause of this is the line Class.forName("..."), so the thrown exception is java.lang.ClassNotFoundException: com.mysql.jdbc.Driver. I have tried thousands of Maven reimports, but nothing helps. Is there anything that I'm missing?

P.S.: similar questions like How to use Maven to Create JSP + Servlet + TOMCAT + MySQL or Where do I have to place the JDBC driver for Tomcat's connection pool? do not solve my problem.

Shadow
  • 33,525
  • 10
  • 51
  • 64
Hidayat Rzayev
  • 339
  • 3
  • 14
  • What's in your webapp's `WEB-INF/lib` directory? Do you see the MySQL driver there? – dnault Jul 11 '20 at 19:06
  • @dnault if I add the MySQL driver there, the URL won't open at all. And I get this `org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.` Eventually Intellij says that the specified URL could not be found. – Hidayat Rzayev Jul 11 '20 at 19:15

1 Answers1

1

Try in url for localhost port 3306 not 80.

And if you get an error whit time zone, use this string :


String url = "jdbc:mysql://127.0.0.1:3306/DemoJSP";
String timezone = "?useUnicode=true&useJDBCCompliantTimezoneShift=true&useLegacyDatetimeCode=false&serverTimezone=UTC";
String username = "root";
String password = "";
Connection connection = DriverManager.getConnection(url + timezone, username, password);
Eduard A
  • 370
  • 3
  • 9
  • Wow, I can't believe the port number was the problem. I changed it to **3306** and it worked! Thanks a lot, I've been struggling with this error the whole day.. – Hidayat Rzayev Jul 11 '20 at 19:20
  • your database use the port 3306 for localhost – Eduard A Jul 11 '20 at 19:25
  • 1
    This can't have been the solution to the problem, because changing this won't fix the `ClassNotFoundException` that the OP reported. – Mark Rotteveel Jul 12 '20 at 15:58
  • 1
    Yes, and I'm saying that that change can't fix the `ClassNotFoundException`, so the OP had a different problem than described in the question, or did something else as well that fixed the `ClassNotFoundException`, and this fixes the next problem they were confronted by. – Mark Rotteveel Jul 13 '20 at 14:27