与Java(Servlet)进行MySQL连接的ClassNotFoundException

时间:2017-05-01 11:17:21

标签: java mysql eclipse servlets

我将Eclipse作为我的IDE,tomcat服务器9.0,MySQL都在Windows 10中运行。

我创建了一个简单的项目来连接MySQL中的一个简单数据库,当我创建一个JAVA PROJECT时代码运行完美,但是当我在动态Web下创建一个 servlet 时,相同的代码失败了项目

以下是代码,问题是 Class.forName

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Connection conn = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost/amir","root","1234");
        } catch (SQLException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        response.setContentType("text/html");
        PrintWriter p = response.getWriter();
        p.println ("<html><body><h3>Hi</h3></body></html>");
    }

这是 StackTrace

  

java.lang.ClassNotFoundException:com.mysql.jdbc.Driver       在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1275)       在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1109)       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Unknown Source)       at myServlet.DynamicWeb.doGet(DynamicWeb.java:36)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:635)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:742)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:475)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)       at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:624)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:342)       在org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:498)       在org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)       at org.apache.coyote.AbstractProtocol $ ConnectionHandler.process(AbstractProtocol.java:796)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1368)       在org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)       at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)       at java.util.concurrent.ThreadPoolExecutor $ Worker.run(Unknown Source)       at org.apache.tomcat.util.threads.TaskThread $ WrappingRunnable.run(TaskThread.java:61)       在java.lang.Thread.run(未知来源)

1 个答案:

答案 0 :(得分:2)

你得到了类未找到的异常,因为包中的那些类不在WEB-INF / lib文件夹中。

您必须手动将项目中使用/包含的所有库或ja​​r复制到WEB-INF / lib文件夹。

在这种情况下,将连接到db所需的所有jar或库文件复制到动态Web项目的WEB-INF / lib文件夹中,它必须正常工作。