【发布时间】:2020-11-01 07:40:11
【问题描述】:
我是 JSP 的新手,所以我正在玩弄它。我在 Intellij 中创建了一个 Maven 项目,并在我的pom.xml 中导入了我需要的依赖项,即mysql-connector 和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>
我有一个使用相应驱动程序访问本地 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 + MySQL 或Where 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