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