簡體   English   中英

Tomcat 通過 Maven 導入后找不到 JDBC 驅動程序

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

我是 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>

我有一個 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);
%>

但是,當我運行 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 . 我已經嘗試了數千次 Maven 重新導入,但沒有任何幫助。 有什么我想念的嗎?

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? 不要解決我的問題。

在 url 中嘗試localhost端口3306而不是 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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM