简体   繁体   中英

Tomcat does not find JDBC Driver after importing via Maven

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?

PS: 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.

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM