JavaWeb项目“java.lang.ClassNotFoundException: com.mysql.jdbc.Driver”异常的解决方法

我们做JavaWeb项目连接MySQL数据库时Eclipse控制台如果出现类似下面的异常提示:

java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1647)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1493)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:186)
at com.google.model.ConnDB.getConn(ConnDB.java:16)
at com.google.model.GoodsBeanBO.getGoodsBean(GoodsBeanBO.java:23)
at com.google.servlet.showGoodsClServlet.doGet(showGoodsClServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
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(Thread.java:722)
java.lang.NullPointerException
at com.google.model.GoodsBeanBO.getGoodsBean(GoodsBeanBO.java:25)
at com.google.servlet.showGoodsClServlet.doGet(showGoodsClServlet.java:41)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
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(Thread.java:722)

这个异常提示说明Eclipse没有找到数据库驱动。

解决办法:

把mysql-connector-java-5.1.7-bin.jar复制到tomcat的lib目录下面!


注意,在普通的Java项目(不是JavaWeb项目)里面,我们只要在项目中引入数据库驱动的jar包(比如mysql-connector-java-5.1.7-bin.jar),代码编写正确一般就可以正常连接数据库。

普通Java项目引入jar包的方法:

右键项目->Properties->Java Build Path(左侧菜单)->选择Libraries
有两种方式,导入JAR包实际上就是建立一种链接,并不是COPY式的导入
一、导入外部包,Add External JARs...,这种只是与JAR包的绝对路径建立链接,并不会拷贝到项目中。
二、导入包,Add JARs...,这种需要你事先把JAR包拷贝到项目中,通过选择项目下的JAR,与JAR包的相对路径建立链接。
两种方式下导入的包,JAR包名称前面的图标会有区别。另外,如果JAR包名称前面的图标出现黄色叹号,说明这个JAR的路径有问题,需要重新导入。

但是在JavaWeb项目中,当你发现按照普通Java项目引入mysql-connector-java-5.1.7-bin.jar后,数据库不好使,且eclipse控制台报错Class.forName("om.mysql.jdbc.Driver"),这时需要把mysql-connector-java-5.1.7-bin.jar复制到tomcat的lib目录下面!

你可能感兴趣的:(JavaWeb笔记)