一尘不染

为什么我找不到Class.forName(“ com.mysql.jdbc.Driver”)的异常?[重复]

mysql

这个问题已经在这里有了答案

如何在Eclipse Web项目中安装JDBC驱动程序而不面对java.lang.ClassNotFoundexception
(13个答案)

3年前关闭。

我正在开发一个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:org.apache.catalina.loader.WebappClassLoaderBase.loadClass(com.mysql.jdbc.Driver)org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1332)
WebappClassLoaderBase.java:1166)at java.lang.Class.forName0(本机方法)at
java.lang.Class.forName(未知源)at model.InfoModel.SaveInfo(InfoModel.java:14)at
controller.Register_user.doGet(
org.apache.catalina.core.ApplicationFilterChain处的javax.servlet.http.HttpServlet.service(HttpServlet.java:622)处的Register_user.java:35)(javax.servlet.http.HttpServlet.service(HttpServlet.java:729)处)
org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)处的.internalDoFilter(ApplicationFilterChain.java:291)。org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain)上的org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:239)上的tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:219)处org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:106)处org.apache.catalina.authenticator
org.apache.catalina.core.StandardHostValve.invoke(AuthenticatorBase.java:502)位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)位于org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
org.apache.catalina.core上的org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:616)。org上org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)上的org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)上的StandardEngineValve.invoke(StandardEngineValve.java:88)
org.apache.tomcat.util.net.NioEndpoint $
SocketProcessor.doRun(NioEndpoint.java:1526)的.apache.coyote.AbstractProtocol
$
AbstractConnectionHandler.process(AbstractProtocol.java:673)在org.apache.tomcat.util.net
.NioEndpoint $
SocketProcessor.run(NioEndpoint.java:1482)在org.apache.tomcat处的java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)在java.util.concurrent.ThreadPoolExecutor
$ Worker.run(未知源) util.threads.TaskThread $
WrappingRunnable.run(TaskThread.java:61)at
java.lang.Thread.run(未知源)java:88)位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)位于org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)位于org.apache.coyote.AbstractProtocol
org.apache.tomcat.util.net.NioEndpoint $
SocketProcessor.doRun(NioEndpoint.java:1526)处的$
AbstractConnectionHandler.process(AbstractProtocol.java:673)在org.apache.tomcat.util.net.NioEndpoint
$ SocketProcessor.run
(NioEndpoint.java:1482),位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源),位于java.util.concurrent.ThreadPoolExecutor
$ Worker.run(未知源),位于org.apache.tomcat.util.threads.TaskThread
$在java.lang.Thread.run处WrappingRunnable.run(TaskThread.java:61)(未知源)java:88)位于org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:518)位于org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)位于org.apache.coyote.AbstractProtocol
org.apache.tomcat.util.net.NioEndpoint $
SocketProcessor.doRun(NioEndpoint.java:1526)处的$
AbstractConnectionHandler.process(AbstractProtocol.java:673)在org.apache.tomcat.util.net.NioEndpoint
$ SocketProcessor.run
(NioEndpoint.java:1482),位于java.util.concurrent.ThreadPoolExecutor.runWorker(未知源),位于java.util.concurrent.ThreadPoolExecutor
$ Worker.run(未知源),位于org.apache.tomcat.util.threads.TaskThread
$在java.lang.Thread.run处WrappingRunnable.run(TaskThread.java:61)(未知源)java:518)位于org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)位于org.apache.coyote.AbstractProtocol
$
AbstractConnectionHandler.process(AbstractProtocol.java:673)位于org.apache.tomcat.util
org.apache.tomcat.util.net.NioEndpoint $
SocketProcessor.run(NioEndpoint.java:1482)处的.net.NioEndpoint $
SocketProcessor.doRun(NioEndpoint.java:1526)在java.util.concurrent.ThreadPoolExecutor.runWorker(未知源代码)在java.util.concurrent.ThreadPoolExecutor
$ Worker.run(未知源)在org.apache.tomcat.util.threads.TaskThread $
WrappingRunnable.run(TaskThread.java:61)在java.lang.Thread.run(来源不明)java:518)位于org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1091)位于org.apache.coyote.AbstractProtocol
$
AbstractConnectionHandler.process(AbstractProtocol.java:673)位于org.apache.tomcat.util
org.apache.tomcat.util.net.NioEndpoint $
SocketProcessor.run(NioEndpoint.java:1482)处的.net.NioEndpoint $
SocketProcessor.doRun(NioEndpoint.java:1526)在java.util.concurrent.ThreadPoolExecutor.runWorker(未知源代码)在java.util.concurrent.ThreadPoolExecutor
$ Worker.run(未知源)在org.apache.tomcat.util.threads.TaskThread $
WrappingRunnable.run(TaskThread.java:61)在java.lang.Thread.run(来源不明)org.apache.tomcat.util.net.NioEndpoint
$ SocketProcessor上的AbstractProtocol $
AbstractConnectionHandler.process(AbstractProtocol.java:673)在org.apache.tomcat.util.net.NioEndpoint
$
SocketProcessor.doRun(NioEndpoint.java:1526)在org.apache.tomcat.util.net.NioEndpoint
$ SocketProcessor
org.apache.tomcat.util.threads.TaskThread处的java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)处运行(NioEndpoint.java:1482)java.util.concurrent.ThreadPoolExecutor
$ Worker.run(未知源)处的java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)
java.lang.Thread.run上的$
WrappingRunnable.run(TaskThread.java:61)(未知源)org.apache.tomcat.util.net.NioEndpoint
$ SocketProcessor上的AbstractProtocol $
AbstractConnectionHandler.process(AbstractProtocol.java:673)在org.apache.tomcat.util.net.NioEndpoint
$
SocketProcessor.doRun(NioEndpoint.java:1526)在org.apache.tomcat.util.net.NioEndpoint
$ SocketProcessor
org.apache.tomcat.util.threads.TaskThread处的java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)处运行(NioEndpoint.java:1482)java.util.concurrent.ThreadPoolExecutor
$ Worker.run(Unknown
Source)处的java.util.concurrent.ThreadPoolExecutor.runWorker(未知源)
java.lang.Thread.run上的$
WrappingRunnable.run(TaskThread.java:61)(未知源)java.util.concurrent.ThreadPoolExecutor
$ Worker.run中的runWorker(未知源)org.apache.tomcat.util.threads.TaskThread $
WrappingRunnable.run(TaskThread.java:61)中的runWorker(未知源)
.run(来源不明)java.util.concurrent.ThreadPoolExecutor $
Worker.run中的runWorker(未知源)org.apache.tomcat.util.threads.TaskThread $
WrappingRunnable.run(TaskThread.java:61)中的runWorker(未知源) .run(来源不明)


阅读 534

收藏
2020-05-17

共1个答案

一尘不染

右键单击该项目,转到PropertiesDeployment Assembly从左窗格中选择(如果找不到,请阅读更新部分)。检查是否Java Build Path Entries提供。如果没有,请单击Add并包含它。如果已经存在,请单击Add以将连接器jar包括为Archive。做一个Project>
Clean,重新启动服务器。

更新资料

如果未对项目进行分面,则将找不到此选项。您可以单击Project Facets,将其转换为多面结构,并添加Dynamic Web Module和相关的依赖项。

更新资料

Web部署程序集问题。在大多数情况下,此答案很有帮助。

2020-05-17