title: ClassNotFoundException: com.mysql.jdbc.Driver. JDBC MySQL driver for web application
body: <p>So I have a .jar file for the MySQL JDBC driver, it is under my libraries source folder and I have the following code:</p>
  
  <pre><code>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;
  
      }
  </code></pre>
  
  <p>However, it always gives me this error:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Here's how I added the jar file. I created a lib folder and put the jar inside and then I go to configure build path in eclipse and added the jar inside the lib folder to the project.</p>