简体   繁体   English

Tomcat 通过 Maven 导入后找不到 JDBC 驱动程序

[英]Tomcat does not find JDBC Driver after importing via Maven

I'm new to JSP, so I'm playing around with it a bit.我是 JSP 的新手,所以我正在玩弄它。 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 :我在 Intellij 中创建了一个 Maven 项目,并在我的pom.xml中导入了我需要的依赖项,即mysql-connectorservlet-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.我有一个 JSP 文件,它使用相应的驱动程序访问本地 MySQL 数据库。

<%
    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 .但是,当我运行 Tomcat 服务器时,我得到HTTP Status 500 The cause of this is the line Class.forName("...") , so the thrown exception is java.lang.ClassNotFoundException: com.mysql.jdbc.Driver . 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.我已经尝试了数千次 Maven 重新导入,但没有任何帮助。 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? 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.在 url 中尝试localhost端口3306而不是 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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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