Eu tentei algumas formas de abrir conexão em uma servlet pra adicionar alguns dados no mysql mas deu esse erro: HTTP Status 500 - java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/clientes_banco
Você está vendo a versão anterior da nova experiência da Alura que estamos preparando para você. Em breve, ela ganha uma identidade visual novinha totalmente pensada em potencializar seus estudos!
Eu tentei algumas formas de abrir conexão em uma servlet pra adicionar alguns dados no mysql mas deu esse erro: HTTP Status 500 - java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/clientes_banco
Provavelmente o jar do MySQL ou alguma de suas dependências estão faltando no Classpath. Poderia colocar no posto como está a estrutura do seu projeto?
ConnectionFactory package br.com.alura.gerenciador;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException;
public class ConnectionFactory {
public Connection getConnection() { try { return DriverManager.getConnection("jdbc:mysql://localhost/clientes_banco", "root", "root"); } catch (SQLException e) { throw new RuntimeException(e); }
} }
Dao
package br.com.alura.gerenciador.dao;
import java.sql.Connection;
import br.com.alura.gerenciador.ConnectionFactory;
public class ClienteDao { private Connection connection;
public ClienteDao() { this.connection = new ConnectionFactory().getConnection(); }
}
Controller
package br.com.alura.gerenciador.web;
import java.io.IOException;
import javax.servlet.RequestDispatcher; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
@WebServlet(urlPatterns = "/executa") public class Controller extends HttpServlet { @Override protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String tarefa = req.getParameter("tarefa");
if (tarefa == null) { throw new IllegalArgumentException("Você esqueceu de passar a tarefa"); } tarefa = "br.com.alura.gerenciador.web." + tarefa; try { Class<?> tipo = Class.forName(tarefa); Tarefa instancia = (Tarefa) tipo.newInstance(); String pagina = instancia.executa(req, resp); RequestDispatcher dispatcher = req.getRequestDispatcher(pagina); dispatcher.forward(req, resp); } catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) { throw new ServletException(); }
} }
Esse aqui foi o teste realizado
package br.com.alura.gerenciador.web;
import java.sql.Connection; import java.sql.SQLException; import java.util.Collection;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse;
import br.com.alura.gerenciador.ConnectionFactory; import br.com.alura.gerenciador.Empresa; import br.com.alura.gerenciador.dao.ClienteDao; import br.com.alura.gerenciador.dao.EmpresaDAO;
public class BuscaEmpresa implements Tarefa {
public BuscaEmpresa() throws SQLException { System.out.println("Construindo uma servlet do tipo BuscaEmpresa " + this); Connection connection = new ConnectionFactory().getConnection(); connection.close(); System.out.println("Conectado!"); }
@Override public String executa(HttpServletRequest req, HttpServletResponse resp) { String filtro = req.getParameter("filtro"); Collection empresas = new EmpresaDAO().buscaPorSimilaridade(filtro); req.setAttribute("empresas", empresas); return "/WEB-INF/paginas/buscaEmpresa.jsp";
} }
O erro foi esse abaixo
GRAVE: Servlet.service() for servlet [br.com.alura.gerenciador.web.Controller] in context with path [/gerenciador] threw exception java.lang.RuntimeException: java.sql.SQLException: No suitable driver found for jdbc:mysql://localhost/clientes_banco
Consegui resolver, eu tinha colocado o drive no projeto mas não na lib na pasta WEB_INF, só depois que adicionei funcionou. Obrigado pela ajuda.