How to fix the error: HTTP Status 500 - Servlet execution threw an exception?

4

Language: Java Server: Apache Tomcat. Environment: Eclipse.

I'm following Caelum's Handbook for course FJ21 - Web Development with Java. I created a form but at the time of writing it presents the following error:

  

HTTP Status 500 - Servlet execution threw an exception

     

type Exception report

     

message Servlet execution threw an exception

     

description The server encountered an internal error that prevented it from fulfilling this request.

     

exception

     

javax.servlet.ServletException: Servlet execution threw an exception       org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)   root cause

     

java.lang.Error: Unresolved compilation problem:     Unhandled exception type SQLException

     

br.com.caelum.jdbc.dao.ContatoDAO (ContactDAO.java:19)

     
    

br.com.caelum.agenda.servlet.AddingContactServlet.service (AddContactServlet.java:46)       javax.servlet.http.HttpServlet.service (HttpServlet.java:729)       org.apache.tomcat.websocket.server.WsFilter.doFilter (WsFilter.java:53)     note The full stack trace of the root cause is available in the Apache Tomcat / 9.0.0.M15 logs.

  

Follow the code of the ContactData class:

public class ContatoDAO {
  private Connection connection;

  public ContatoDAO()  {
    try {
      this.connection = new ConnectionFactory().getConnection();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
  }

  public void adiciona(Contato contato)  {
      String sql = "insert into contatos (nome, email, endereco, dataNascimento) values (?,?,?,?)";

      try {
        PreparedStatement stmt = connection.prepareStatement(sql);

        stmt.setString(1, contato.getNome());
        stmt.setString(2, contato.getEmail());
        stmt.setString(3, contato.getEndereco());
        stmt.setDate(4, new Date(contato.getDataNascimento().getTimeInMillis()));

        stmt.execute();
        stmt.close();
      } catch (SQLException e)  {
        throw new RuntimeException(e);
      }
  }
}

Contact Class:

public class Contato {
  private Long id;
  private String nome;
  private String email;
  private String endereco;
  private Calendar dataNascimento;
  public Long getId() {
    return id;
  }
  public void setId(Long id) {
    this.id = id;
  }
  public String getNome() {
    return nome;
  }
  public void setNome(String nome) {
    this.nome = nome;
  }
  public String getEmail() {
    return email;
  }
  public void setEmail(String email) {
    this.email = email;
  }
  public String getEndereco() {
    return endereco;
  }
  public void setEndereco(String endereco) {
    this.endereco = endereco;
  }
  public Calendar getDataNascimento() {
    return dataNascimento;
  }
  public void setDataNascimento(Calendar dataNascimento) {
    this.dataNascimento = dataNascimento;
  }
}
    
asked by anonymous 16.01.2017 / 00:14

1 answer

0

The JVM is accusing that the SQLException exception is not being handled in any of its methods in the ContactData class:

  

java.lang.Error: Unresolved compilation problem: Unhandled exception   type SQLException

However, the code you posted seems ok. It may be that the code that is deployed on the server is in error and the IDE has not done hotdeploy. Check your code again to make sure there is no syntax error and clean build your code. Preferably, delete the .war from the server and do a clean build and a complete redeploy.

    
20.10.2017 / 13:47