首页 > 解决方案 > 在 Eclipse 中使用 JBDC 驱动程序给了我一个 classNotFoundException

问题描述

这是我第一次使用 JSP。我已经设置了一个 tomcat 9.0.8 服务器,我正在使用 Java 8.5,以及 MySQL 8.0.11,我在 Windows 10 上使用 Eclipse EE。

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver

我遇到了这个错误,而且我看到很多其他人问过同样的问题,所以我尝试了很多解决方案,但都没有奏效。我已经下载了 mysql-connector-java-8.0.11这些是我迄今为止尝试过的一些解决方案:

  1. <%@ page import = "com.mysql.jbdc.Driver %>在文件开头添加
  2. 都试过Class.forName("com.mysql.jdbc.Driver");Class.forName("com.mysql.jdbc.Driver").newInstance();
  3. 将 JAR 文件添加到:

    1. apache-tomcat-9.0.8\lib
    2. ..\eclipse-workspace\ProjectFolder
    3. ..\eclipse-workspace\ProjectFolder\WEB_INF\lib\
  4. 在项目的 BUILD PATH 中添加为外部 JAR

请帮忙。我不知道还能做什么。

编辑:这是我当前的代码。

<%@
    page import="java.sql.*, javax.sql.*"
%>

<%
    String name = request.getParameter("name");
    String age = request.getParameter("age");
    Class.forName("com.mysql.jdbc.Driver");
    java.sql.Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "user", "pass");
    Statement s = con.createStatement();
%>

<html>
<head>
    <title> Processing </title>
</head>
<body>
    Hello <%=name%>
</body>
</html>

编辑:

我通过在构建路径选项的“订单和导出”设置中选择 JAR 来使其工作。

标签: javamysqleclipsejspjdbc

解决方案


在 Eclipse 中:

  1. 右键单击您的项目
  2. 选择属性
  3. 选择 Java Build Path(从打开的对话框的左侧边栏中)
  4. 单击库(在右侧边栏上)
  5. 单击添加外部 JAR
  6. 选择您的 MySQL 连接器 Jar 并单击打开。

现在应用并重新运行您的代码。

供参考:http ://www.oxfordmathcenter.com/drupal7/node/44


推荐阅读