【问题标题】:Tomcat does not find JDBC Driver after importing via MavenTomcat通过Maven导入后找不到JDBC Driver
【发布时间】:2020-11-01 07:40:11
【问题描述】:

我是 JSP 的新手,所以我正在玩弄它。我在 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>

我有一个使用相应驱动程序访问本地 MySQL 数据库的 JSP 文件。

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

但是,当我运行 Tomcat 服务器时,我得到了HTTP Status 500。造成这种情况的原因是Class.forName("...") 行,所以抛出的异常是java.lang.ClassNotFoundException: com.mysql.jdbc.Driver。我已经尝试了数千次 Maven 重新导入,但没有任何帮助。有什么我遗漏的吗?

附注:类似的问题,如How to use Maven to Create JSP + Servlet + TOMCAT + MySQLWhere do I have to place the JDBC driver for Tomcat's connection pool? 并不能解决我的问题。

【问题讨论】:

  • 你的 webapp 的 WEB-INF/lib 目录中有什么?你看到那里的 MySQL 驱动程序了吗?
  • @dnault 如果我在那里添加 MySQL 驱动程序,则 URL 根本不会打开。我得到了这个 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. 最终 Intellij 说找不到指定的 URL。

标签: java maven jsp jdbc mysql-connector


【解决方案1】:

localhost 端口 3306 的 url 中尝试,而不是 80。

如果您遇到时区错误,请使用此字符串:


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

【讨论】:

  • 哇,我不敢相信端口号是问题所在。我将其更改为 3306 并且有效!非常感谢,我整天都在为这个错误而苦苦挣扎..
  • 您的数据库使用 3306 端口作为 localhost
  • 这不可能是问题的解决方案,因为更改它不会修复 OP 报告的 ClassNotFoundException
  • 是的,我是说该更改无法修复 ClassNotFoundException,因此 OP 的问题与问题中描述的不同,或者还做了其他修复 @ 987654324@,这解决了他们面临的下一个问题。
猜你喜欢
  • 2019-04-10
  • 2012-02-06
  • 2019-03-25
  • 2017-08-14
  • 2023-03-05
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 2022-01-03
相关资源
最近更新 更多