title: Connect Servlet to MySQL database in eclipse
body: <p>I successfully connected database with simple java program using JDBC but when I am trying to connect database with Servlet, it gives me following errors and exceptions:</p>
  
  <pre><code>java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost:8888/ebookshop
      at java.sql.DriverManager.getConnection(Unknown Source)
      at java.sql.DriverManager.getConnection(Unknown Source)
      at QueryServlet.doGet(QueryServlet.java:35)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:618)
      at javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
      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:505)
      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:610)
      at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
      at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:534)
      at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1081)
      at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:658)
      at org.apache.coyote.http11.Http11NioProtocol$Http11ConnectionHandler.process(Http11NioProtocol.java:222)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1566)
      at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1523)
      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)
  </code></pre>
  
  <p><strong>All severs running well and also I already added servlet and connecter API's in my library.</strong></p>
  
  <p>For more Information these are my html and servlet files are:</p>
  
  <p><strong>html file</strong></p>
  
  <pre class="lang-html prettyprint-override"><code>&lt;html&gt;
      &lt;head&gt;&lt;title&gt;Yet Another Bookshop&lt;/title&gt;&lt;/head&gt;
  
      &lt;body&gt;
          &lt;h2&gt;Yet Another Bookshop&lt;/h2&gt;
          &lt;form method="get" action="http://localhost:9999/Sixth/query"&gt;
              &lt;b&gt;Choose an author:&lt;/b&gt;
              &lt;input type="checkbox" name="author" value="Tan Ah Teck"&gt;Ah Teck
              &lt;input type="checkbox" name="author" value="Mohammad Ali"&gt;Ali
              &lt;input type="checkbox" name="author" value="Kumar"&gt;Kumar
              &lt;input type="submit" value="Search"&gt;
          &lt;/form&gt;
      &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>My servlet:</p>
  
  <pre><code>import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.Connection;
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.sql.Statement;
  
  import javax.servlet.ServletException;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  public class QueryServlet extends HttpServlet {
  
      private static final long serialVersionUID = 1L;
  
      // JDK 6 and above only
      // The doGet() runs once per HTTP GET request to this servlet.
      @Override
      public void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          // Set the MIME type for the response message
          response.setContentType("text/html");
          // Get a output writer to write the response message into the network socket
          PrintWriter out = response.getWriter();
  
          Connection conn = null;
          Statement stmt = null;
  
          try {
              // Step 1: Allocate a database Connection object
              Class.forName("com.mysql.jdbc.Driver");
              conn = DriverManager.getConnection(
                      "jdbc:mysql://localhost:8888/ebookshop", "myuser", "xxxx"); // &lt;== Check!
              // database-URL(hostname, port, default database), username, password
  
              // Step 2: Allocate a Statement object within the Connection
              stmt = conn.createStatement();
  
              // Step 3: Execute a SQL SELECT query
              String sqlStr = "select * from books where author = "
                      + "'" + request.getParameter("author") + "'"
                      + " and qty &gt; 0 order by price desc";
  
              // Print an HTML page as the output of the query
              out.println("&lt;html&gt;&lt;head&gt;&lt;title&gt;Query Response&lt;/title&gt;&lt;/head&gt;&lt;body&gt;");
              out.println("&lt;h3&gt;Thank you for your query.&lt;/h3&gt;");
              out.println("&lt;p&gt;You query is: " + sqlStr + "&lt;/p&gt;"); // Echo for debugging
              ResultSet rset = stmt.executeQuery(sqlStr);  // Send the query to the server
  
              // Step 4: Process the query result set
              int count = 0;
              while (rset.next()) {
                  // Print a paragraph &lt;p&gt;...&lt;/p&gt; for each record
                  out.println("&lt;p&gt;" + rset.getString("author")
                          + ", " + rset.getString("title")
                          + ", $" + rset.getDouble("price") + "&lt;/p&gt;");
                  count++;
              }
              out.println("&lt;p&gt;==== " + count + " records found =====&lt;/p&gt;");
              out.println("&lt;/body&gt;&lt;/html&gt;");
          } catch (SQLException ex) {
              ex.printStackTrace();
          } catch (ClassNotFoundException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
      }
  }
  </code></pre>