0

I have a problem in the JSF + JDBC project, more specifically in my servlet, I can't call the DAO method and it gives me the following error

Error connecting: org.gjt.mm.mysql.Driver
abr 14, 2020 12:29:11 PM org.apache.catalina.core.StandardWrapperValve invoke
GRAVE: Servlet.service() for servlet [br.com.cuponsdesconto.controllers.CadastrarUsuario] in context with path [/WebCupom] threw exception

An interesting fact is that if I create a test class and call my DAO method, it works normally, see below

package br.com.cuponsdesconto.controllers;

import br.com.cuponsdesconto.dao.UsuarioDao;
import br.com.cuponsdesconto.entidades.Usuario;

public class TestaInsercaoDeRegistro {
    public static void main(String[] args) {
        Usuario u = new Usuario();
        u.setCpf("123");
        u.setEmail("teste");
        u.setNome("Arthur");
        u.setSenha("123");

        new UsuarioDao().adicionar(u);
    }
}

That is, the problem is not with the database connection, but why can't I use my DAO in the Servlet?

I'll show you how my files are

My Servlet:

package br.com.cuponsdesconto.controllers;

import java.io.IOException;
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 br.com.cuponsdesconto.dao.UsuarioDao;
import br.com.cuponsdesconto.entidades.Usuario;

@WebServlet("/CadastrarUsuario")
public class CadastrarUsuario extends HttpServlet {
    private static final long serialVersionUID = 1L;

    public CadastrarUsuario() {
        super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("I'm here!");

        String nome = request.getParameter("nome");
        String cpf = request.getParameter("cpf");
        String email = request.getParameter("email");
        String senha = request.getParameter("senha");

        Usuario usuario = new Usuario();
        usuario.setNome(nome);
        usuario.setCpf(cpf);
        usuario.setEmail(email);
        usuario.setSenha(senha);

        UsuarioDao dao = new UsuarioDao();
        dao.adicionar(usuario);

        response.getWriter().append("Usuario cadastrado").append(request.getContextPath());
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

}

(That sout appears on the console so this is a good sign)

Now let's see my web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
  <session-config>
    <session-timeout>
        30
    </session-timeout>
  </session-config>
  <welcome-file-list>
    <welcome-file>index.jps</welcome-file>
  </welcome-file-list>
</web-app>

And finally, this is my UsuarioDao

package br.com.cuponsdesconto.dao;

import br.com.cuponsdesconto.entidades.Entidade;
import br.com.cuponsdesconto.entidades.Usuario;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class UsuarioDao extends Dao implements FuncoesDao {

    @Override
    public boolean adicionar(Entidade entidade) {
        Usuario usuario = (Usuario) entidade;
        String sql = "insert into usuario (nome, cpf, email, senha) values (?,?,?,?)";
        try{
            this.conectar();
            this.stmt = this.conn.prepareStatement(sql);
            this.stmt.setString(1, usuario.getNome());
            this.stmt.setString(2, usuario.getCpf());
            this.stmt.setString(3, usuario.getEmail());
            this.stmt.setString(4, usuario.getSenha());
            this.stmt.execute();
            System.out.println("Usuario criado com sucesso!");
        } catch(SQLException ex){
            System.out.println("Erro ao inserir Usuario "+ex.getMessage());
            return false;
        }
        finally{
            try {
                this.conn.close();
            } catch (SQLException ex) {
                Logger.getLogger(UsuarioDao.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return true;
    }

I've searched everywhere, I can't find the answer to this, please help me

Arthur
  • 3
  • 4

1 Answers1

0

A solution to my problem is simple, JSF is unable to connect to the database because the WebContent (folder that stores the front end) did not have a connector in the WEB-INF / lib folder , I added a mysql-connector.jar and it worked.

When I create a test in / src (a folder that stores or backend), it works because it is part of the project that had a connector.

I didn't know that precisely 2 mysql connectors for the executed project

Arthur
  • 3
  • 4