Eclipse - 将.jar添加到动态 Web 项目
当我在eclipse里面使用下面的代码时,就像这样:Dynamic Web Project
servlet
@WebServlet("/CreateCustomerServlet")
public class CreateCustomerServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private void test() throws SQLException, ClassNotFoundException {
// use the JAR ...
Class.forName("com.mysql.jdbc.Driver");//here the exception
}
}
它引发异常:
java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
在行中:
Class.forName("com.mysql.jdbc.Driver");
在动态 Web 项目的构建路径中,我添加了 jar:
mysql-connector-java-5.1.21-bin.jar
我添加了文件夹的快照,您可以看到它在哪里是所需的jar -