gpt4 book ai didi

java - 为什么我遇到 Class.forName ("com.mysql.jdbc.Driver"); 找不到类异常

转载 作者:行者123 更新时间:2023-12-01 19:38:29 29 4
gpt4 key购买 nike

我正在开发一个网络应用程序,使用: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 。但为什么它给了我这样一个异常(exception)?

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

最佳答案

右键单击该项目,转到属性。从左侧 Pane 中选择部署程序集(如果未找到,请阅读更新部分)。检查是否提供了Java 构建路径条目。如果没有,请单击添加并将其包含在内。如果它已经存在,请单击添加以将您的连接器 jar 包含为存档。执行项目>清理,重新启动服务器。

更新

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

更新

Web 部署程序集问题。这个答案在大多数情况下都是有帮助的。

关于java - 为什么我遇到 Class.forName ("com.mysql.jdbc.Driver"); 找不到类异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32602992/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com