0

I'm having issues connecting to a web based (not local) MySQL database. I keep getting the error: No suitable driver found for jdbc:mysql://host:port/database (I'm using the actual host of course) in the console. The webpage just displays "null".

I am brand new to Java so was using this post 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.

I'm using Tomcat 7 into Eclipse IDE.

I've been Googling for the past couple hours with no luck... please help! Here is the code I'm using:

Connect.java

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);
    }
}

Employees.java

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;
    }

}

The servlet I'm trying to run

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
    }

}

I've also created a simple connection test class that is working and not returning no suitable driver error.

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);
        }
    }

}
Community
  • 1
  • 1
doriansm
  • 247
  • 1
  • 5
  • 31

1 Answers1

0

The issue was in the Connect.java class. I added the following line of code DriverManager.registerDriver(new com.mysql.jdbc.Driver ()); right before Connection conn = (Connection) DriverManager.getConnection(url, username, password);

It's now working!

doriansm
  • 247
  • 1
  • 5
  • 31
  • You should never call `registerDriver` yourself. If the driver is not automatically loaded, then all you should do is use `Class.forName("com.mysql.jdbc.Driver")`. Calling `registerDriver` is only done by JDBC drivers themselves. – Mark Rotteveel Apr 03 '17 at 07:47