@renatorigatto escreveu:
Olá,
Estou aprendendo Java Web e fazendo um projeto de Agenda, acompanhando a apostila do curso FJ-21 da Caelum. Peço a ajuda de vocês para conseguir resolver este problema.
No exercício 9.7 de MVC eu segui os passos exatamente como estão descritos na apostila e quando eu acesso "lista-contatos.jsp" e clico "Remover" em algum contato aparece o erro abaixo:
threw exception [A lógica de negócios causou uma exceção!] with root cause
java.lang.NumberFormatException: For input string: ""Segue abaixo os códigos das classes envolvidas:
package br.com.renato.bean;
import java.util.Calendar;
public class Contato {
private Long id; private String nome; private String email; private String endereco; private Calendar dataNascimento; Getters e Setters... //Getter/Setter - id public Long getId() { return id; } public void setId(Long id) { this.id = id; } //Getter/Setter - nome public String getNome() { return nome; } public void setNome(String nome) { this.nome = nome; } //Getter/Setter - email public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } //Getter/Setter - endereco public String getEndereco() { return endereco; } public void setEndereco(String endereco) { this.endereco = endereco; } //Getter/Setter - dataNascimento public Calendar getDataNascimento() { return dataNascimento; } public void setDataNascimento(Calendar dataNascimento) { this.dataNascimento = dataNascimento; }
}
package br.com.renato.dao;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;import br.com.renato.bean.Contato;
import br.com.renato.connection.ConnectionFactory;public class ContatoDao {
private Connection connection;
public ContatoDao(){ this.connection = new ConnectionFactory().getConnection(); } 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); } } public List<Contato> getLista(){ try{ PreparedStatement stmt = this.connection.prepareStatement("select * from contatos"); List<Contato> contatos = new ArrayList<Contato>(); ResultSet rs = stmt.executeQuery(); while (rs.next()){ Contato contato = new Contato(); contato.setId(rs.getLong("id")); contato.setNome(rs.getString("nome")); contato.setEmail(rs.getString("email")); contato.setEndereco(rs.getString("endereco")); Calendar data = Calendar.getInstance(); data.setTime(rs.getDate("dataNascimento")); contato.setDataNascimento(data); contatos.add(contato); } rs.close(); stmt.close(); return contatos; } catch (SQLException e){ throw new RuntimeException (e); } } public void altera (Contato contato){ String sql = "update contatos set nome=?, email=?, endereco=?, dataNascimento=? where id=?"; 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.setLong(5, contato.getId()); stmt.execute(); stmt.close(); } catch (SQLException e){ throw new RuntimeException (e); } } public void remove (Contato contato){ try{ PreparedStatement stmt = connection.prepareStatement("delete * from contatos where id=?"); stmt.setLong(1, contato.getId()); stmt.execute(); stmt.close(); } catch (SQLException e){ throw new RuntimeException (e); } }
}
package br.com.renato.mvc.logica;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public interface Logica {
String executa (HttpServletRequest request, HttpServletResponse response) throws Exception;
}
package br.com.renato.mvc.servlet;
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.renato.mvc.logica.Logica;
@WebServlet ("/mvc")
public class ControllerServlet extends HttpServlet{@Override protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String parametro = request.getParameter("logica"); String nomeDaClasse = "br.com.renato.mvc.logica." + parametro; try{ Class classe = Class.forName(nomeDaClasse); Logica logica = (Logica) classe.newInstance(); String pagina = logica.executa(request, response); request.getRequestDispatcher(pagina).forward(request, response); } catch (Exception e){ throw new ServletException ("A lógica de negócios causou uma exceção!", e); } }
}
package br.com.renato.mvc.logica;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import br.com.renato.bean.Contato;
import br.com.renato.dao.ContatoDao;public class RemoveContatoLogic implements Logica {
@Override public String executa(HttpServletRequest request, HttpServletResponse response) throws Exception { long id = Long.parseLong(request.getParameter("id")); Contato contato = new Contato(); contato.setId(id); ContatoDao dao = new ContatoDao(); dao.remove(contato); System.out.println("Excluindo contato..."); return "lista-contatos.jsp"; }
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
<table> <c:forEach var="contato" items="${dao.lista}"> <tr> <td>${contato.nome}</td> <td> <c:choose> <c:when test="${not empty contato.email}"> <a href="mailto:${contato.email}">${contato.email}</a> </c:when> <c:otherwise> E-mail não informado </c:otherwise> </c:choose> </td> <td>${contato.endereco}</td> <td><fmt:formatDate value="${contato.dataNascimento.time}" pattern="dd/MM/yyyy"/></td> <td> <a href="mvc?logica=RemoveContatoLogic&id=${contado.id}">Remover</a> </td> </tr> </c:forEach> </table>
Mensagens: 3
Participantes: 2