ClassNotFoundException:com.mysql.jdbc.Driver。Web應(yīng)用程序的JDBC MySQL驅(qū)動(dòng)程序
您錯(cuò)過(guò)了類(lèi)路徑中的mysql-connector-j jar文件。使用“ java -cp。:MysqL.jar …”。哦這是一個(gè)webapp!然后把MysqL jar文件放到WEB-INF / lib文件夾中。
解決方法因此,我有一個(gè)用于MySQL JDBC驅(qū)動(dòng)程序的.jar文件,該文件位于我的庫(kù)源文件夾下,并且具有以下代碼:
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; }
但是,它總是給我這個(gè)錯(cuò)誤:
INFO: Server startup in 645 msjava.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 invokeSEVERE: 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 causejava.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文件的方法。我創(chuàng)建了一個(gè)lib文件夾,并將jar放入其中,然后在eclipse中配置構(gòu)建路徑,并將lib文件夾中的jar添加到了項(xiàng)目中。
相關(guān)文章:
1. macos - 無(wú)法source activate python272. 運(yùn)行python程序時(shí)出現(xiàn)“應(yīng)用程序發(fā)生異常”的內(nèi)存錯(cuò)誤?3. javascript - 微信 H5 授權(quán) 返回鍵4. github - 求助大神啊,win10 git clone error,折騰了幾天都不行,以前原本好好的,突然就這樣了5. android - 如何實(shí)現(xiàn)QQ pad 點(diǎn)擊右側(cè)輸入框,只頂右側(cè)的布局,左側(cè)布局不動(dòng)6. javascript - npm run build后調(diào)用api返回index.html7. css - 關(guān)于background-position百分比的問(wèn)題?8. java - 處理數(shù)據(jù)關(guān)聯(lián)關(guān)系,使用數(shù)據(jù)庫(kù)表外鍵和代碼內(nèi)維護(hù)相比的優(yōu)缺點(diǎn)?9. javascript - node得到req不能得到boolean10. 小白學(xué)python的問(wèn)題 關(guān)于%d和%s的區(qū)別

網(wǎng)公網(wǎng)安備