title: Import MySQL JDBC Driver into Dynamic Web Project
body: <p>I'm having issues connecting to a web based (not local) MySQL database. I keep getting the error: <strong>No suitable driver found for jdbc:mysql://host:port/database</strong> (I'm using the actual host of course) in the console. The webpage just displays "null".</p>
  
  <p>I am brand new to Java so was using <a href="https://stackoverflow.com/questions/30384473/how-to-import-jdbc-driver-into-dynamic-web-project">this post</a>
  as reference to get started. I created a dynamic web project in Eclipse and have added the JDBC connector JAR file under WebContent > WEB-INF > LIB.  I am totally lost as to why this continues to give me an error. </p>
  
  <p>I'm using Tomcat 7 into Eclipse IDE.</p>
  
  <p>I've been Googling for the past couple hours with no luck... please help! Here is the code I'm using:</p>
  
  <p><strong>Connect.java</strong></p>
  
  <pre><code>package dbObjects;
  
  import java.sql.DriverManager;
  import java.sql.ResultSet;
  import java.sql.SQLException;
  
  import com.mysql.jdbc.Connection;
  import com.mysql.jdbc.Statement;
  
  public class Connect {
      protected Connection getConnection() throws SQLException {
          String url = "jdbc:mysql://host:port/database";
          String username = "username";
          String password = "password";
  
          Connection conn = (Connection) DriverManager.getConnection(url, username, password);
          return conn;
      }
  
      protected ResultSet getResultSet(String sql) throws SQLException {
          Connection conn = getConnection();
          Statement st = (Statement) conn.createStatement();
          return st.executeQuery(sql);
      }
  }
  </code></pre>
  
  <p><strong>Employees.java</strong></p>
  
  <pre><code>package dbObjects;
  
  import java.sql.ResultSet;
  import java.sql.SQLException;
  import java.util.Date;
  
  public class Employees extends Connect {
      private String login;
      private String workgroup;
      private String csm;
  
      public Employees(String login, String workgroup, String csm) {
          super();
          this.login = login;
          this.workgroup = workgroup;
          this.csm = csm;
      }
  
      public Employees(String login) throws SQLException {
          super();
          this.login = login;
          setUserById(login);
      }
  
      private void setUserById(String login) throws SQLException {
          ResultSet resultSet = getResultSet("SELECT * FROM employeeData LIMIT 5");
          while(resultSet.next()) {
              System.out.println(resultSet.getString("login"));
              workgroup = resultSet.getString("workgroup");
              csm = resultSet.getString("csm");
          }
      }
  
      @Override
      public String toString() {
          return workgroup;
      }
  
  }
  </code></pre>
  
  <p><strong>The servlet I'm trying to run</strong></p>
  
  <pre><code>package servlets;
  
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.sql.SQLException;
  
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  
  import dbObjects.Employees;
  
  /**
   * Servlet implementation class HomeServlet
   */
  @WebServlet(description = "Home page shown to user", urlPatterns = { "/HomeServlet" })
  public class HomeServlet extends HttpServlet {
      private static final long serialVersionUID = 1L;
  
      /**
       * @see HttpServlet#HttpServlet()
       */
      public HomeServlet() {
          super();
          // TODO Auto-generated constructor stub
      }
  
      /**
       * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
          Employees user = null;
          try {
              user = new Employees(null);
          } catch (SQLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          }
          PrintWriter pw = response.getWriter();
          pw.println(user);
      }
  
      /**
       * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
          // TODO Auto-generated method stub
      }
  
  }
  </code></pre>
  
  <p>I've also created a simple connection test class that is working and not returning no suitable driver error.</p>
  
  <pre><code>import java.sql.DriverManager;
  import java.sql.SQLException;
  
  import com.mysql.jdbc.Connection;
  
  public class connTest {
  
      public static void main(String[] args) {
          // TODO Auto-generated method stub
          String url = "jdbc:mysql://host:port/database";
      String username = "username";
      String password = "password";
  
          System.out.println("Connecting database...");
  
          try (Connection connection = (Connection) DriverManager.getConnection(url, username, password)) {
              System.out.println("Database connected!");
          } catch (SQLException e) {
              throw new IllegalStateException("Cannot connect the database!", e);
          }
      }
  
  }
  </code></pre>