How to fix the error: HTTP Status 500 - Servlet Execution threw an Exception?

Asked

Viewed 5,435 times

4

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

I am following the Caelum Workbook of the course FJ21 - Web Development with Java. I created a form but at the time of recording it presents the following error:

HTTP Status 500 - Servlet Execution threw an Exception

type Exception report

message Servlet Execution threw an Exception

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.ContactoDAO. (Contactodao.java:19)

br.com.Caelum.agenda.Servlet.AdicionaContactsServlet.service(Addresscontact.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 Contactodao 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);
      }
  }
}

Class Contact:

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

    Put the class "Contact"

  • I updated the post with the contact class.

1 answer

1

JVM is charging that the Sqlexception exception is not being handled in any of the methods in its Contactodao class:

java.lang.Error: Unresolved Compilation problem: Unhandled Exception type Sqlexception

however, the code you posted looks ok. It may be that the code that is deployed on the server is in error and the IDE has not hotdeployed. Recheck your code to make sure there is no syntax error and build a clean build of your code. Preferably, delete . War the server and make a clean build and a complete redeploy.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.