参见英文答案 > How to install JDBC driver in Eclipse web project without facing java.lang.ClassNotFoundexception 12个
所以我有一个MysqL JDBC驱动程序的.jar文件,它位于我的库源文件夹下,我有以下代码:
public static Connection getConnection() throws sqlException {
Connection conn = null;
try{
Class.forName("com.MysqL.jdbc.Driver").newInstance();
String url = "jdbc:MysqL://50.56.81.42:3306/GUEST_BOOK";
String user = "user";
String password = "pass";
conn = (Connection) DriverManager.getConnection(url,user,password);
}catch(ClassNotFoundException e){
e.printStackTrace();
}catch(InstantiationException e){
e.printStackTrace();
}catch(IllegalAccessException e){
e.printStackTrace();
}
return conn;
}
但是,它总是给我这个错误:
INFO: Server startup in 645 ms
java.lang.ClassNotFoundException: com.MysqL.jdbc.Driver
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at java.lang.Class.forName0(Native Method)
at java.lang.Class.forName(Class.java:169)
at gbook.DbHelper.getConnection(DbHelper.java:14)
at gbook.DbHelper.getGuestBook(DbHelper.java:51)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:433)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:680)
Dec 1,2011 1:41:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [jsp] in context with path [/JDBC-MysqL] threw exception [javax.servlet.ServletException: java.lang.NoClassDefFoundError: com/MysqL/jdbc/Connection] with root cause
java.lang.ClassNotFoundException: com.MysqL.jdbc.Connection
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1678)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1523)
at gbook.DbHelper.getGuestBook(DbHelper.java:52)
at org.apache.jsp.index_jsp._jspService(index_jsp.java:83)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
这是我添加jar文件的方式.我创建了一个lib文件夹并将jar放入其中然后我在eclipse中配置构建路径并将lib文件夹中的jar添加到项目中.
最佳答案