为什么我得到Class.forName(“com.mysql.jdbc.Driver”);

时间:2015-09-16 08:06:28

标签: java mysql eclipse jdbc

我正在开发一个Web应用程序使用:eclipse IDE(Marse),Java 8,Apache tomcat 8,MySQL。 我的用户注册代码如下:

System.out.println("Connecting....");
            Class.forName("com.mysql.jdbc.Driver");
            System.out.println("Class found....");
            String url="jdbc:mysql://localhost:3306/db";
            String user="root";
            String password="root";
            System.out.println("success");
            Connection conn = DriverManager.getConnection(url,user,password);
            String query = "insert into login (username,password,emailid,country,city) values (?,?,?,?,?)";
            PreparedStatement ps = conn.prepareStatement(query);
            ps.setString(1,b.getUsername());
            ps.setString(2,b.getPassword());
            ps.setString(3,b.getEmailid());
            ps.setString(4,b.getCountry());
            ps.setString(5,b.getCity());
            int count=ps.executeUpdate();
            ps.close();
            conn.close();

我的代码也是正确的,并且我在eclipse中使用 Build path> add_external_jar 选项添加了mysql-connector-java-5.1.36 。但是为什么它给我这样的例外呢?

  

连接....   java.lang.ClassNotFoundException:com.mysql.jdbc.Driver       在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)       在org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1166)       at java.lang.Class.forName0(Native Method)       at java.lang.Class.forName(Unknown Source)       在model.InfoModel.SaveInfo(InfoModel.java:14)       在controller.Register_user.doGet(Register_user.java:35)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:622)       在javax.servlet.http.HttpServlet.service(HttpServlet.java:729)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:291)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)       在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)       在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)       在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)       在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)       在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:502)       在org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:142)       at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)       在org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)       在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)       在org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)       在org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)       at org.apache.coyote.AbstractProtocol $ AbstractConnectionHandler.process(AbstractProtocol.java:673)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.doRun(NioEndpoint.java:1526)       在org.apache.tomcat.util.net.NioEndpoint $ SocketProcessor.run(NioEndpoint.java:1482)       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 :(得分:9)

右键单击项目,转到Properties。从左侧窗格中选择Deployment Assembly(如果未找到,请阅读更新部分)。检查是否提供了Java Build Path Entries。如果没有,请单击Add并将其包含在内。如果已存在,请单击Add以将连接器jar包括为Archive。执行Project> Clean,重新启动服务器。

更新

如果项目没有分面,您将找不到此选项。您可以点击Project Facets,转换为分面结构并添加Dynamic Web Module和相关的依赖项。

更新

Web部署程序集问题。 在大多数情况下,这个答案很有用。