在webservice中找不到类class.forName(“com.mysql.jdbc.Driver”)

时间:2012-04-05 23:39:07

标签: java eclipse web-services classnotfoundexception

我有一个问题,需要帮助Class.forName("com.mysql.jdbc.Driver")当我从eclipse运行webservice时抛出class not found异常,但是当我创建新的java项目时,它运行得很好。

我在两个项目的构建路径中添加mysql-connector-java-5.1.19-bin.jar 但我不知道网络服务中的问题是什么。

  public String insertOrder(
         int current_id, 
        int table_id) 
        {
            try {
      Class.forName("com.mysql.jdbc.Driver");
      Connection con = 
    DriverManager.getConnection("jdbc:mysql://localhost:3306/myhoteldb", "root", "mypassword");
      PreparedStatement st = 
    con.prepareStatement("insert into orders(orders.current_id,orders.table_id) values(?,?)");
      st.setInt(1, current_id);
      st.setInt(2, table_id);

      st.executeUpdate();
      } catch (Exception e) {
      System.out.println(e.getMessage());
      }
      return "record inserted";
      }
    }

这是错误日志

  java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1680)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1526)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Unknown Source)
at pkg.MyServices.insertOrder(MyServices.java:124)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.axis2.rpc.receivers.RPCUtil.invokeServiceClass(RPCUtil.java:194)
at org.apache.axis2.rpc.receivers.RPCMessageReceiver.invokeBusinessLogic(RPCMessageReceiver.java:102)
at org.apache.axis2.receivers.AbstractInOutMessageReceiver.invokeBusinessLogic(AbstractInOutMessageReceiver.java:40)
at org.apache.axis2.receivers.AbstractMessageReceiver.receive(AbstractMessageReceiver.java:114)
at org.apache.axis2.engine.AxisEngine.receive(AxisEngine.java:173)
at org.apache.axis2.transport.http.HTTPTransportUtils.processHTTPPostRequest(HTTPTransportUtils.java:173)
at org.apache.axis2.transport.http.AxisServlet.doPost(AxisServlet.java:144)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:588)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:16)

如果此代码在您的J2SE中有效,则意味着您需要在某处包含com.mysql.jdbc.Driver类的JAR文件(所谓的JDBC驱动程序)。这个JAR需要在Tomcat中可见。因此,我建议将mysql-jdbc.jar放置在项目的/WEB-INF/lib目录的物理位置。

过去很多次,如果jar不在物理位置,我会遇到ClassNotFoundException。然后重启Tomcat应该可以工作。