Estou com um probleminha. Tenho uma tabela no banco de alunos. Criei minha servlet de nome “RemoveAlunoServlet”. Criei o mapeamento no Web.xml com a url-pattern “/removeAluno”, só que minha grande dúvida está na minha “lista-alunos.jsp”. Estou querendo chamar a minha servlet de remoção através de um link chamado de “Remover”. Fiz só que não consegui fazer da forma correta e não está removendo ao clicar no link. E quando clico no link cai no erro HTTP 500 com a seguinte mensagem de erro:
[b]type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
Em servlet.RemoveAlunoServlet.service(RemoveAlunoServlet.java:26)
Você tentou remover um aluno cujo id era “” e não um Long. Deu erro de cast.
Veja se todos os objetos retornados por ${dao.lista} possuem ids válidos.
Ids que não podem ser convertidos em longs não são válidos (ex: null, “abc12”, “”).
Sim…é justamente no link. Tanto é que que ta dando aquela exception input string = “”. Daquele HTTP 500 mencionado no início. Aquela EL (${aluno.id}), não está pegando um id válido…
Esses são os 2 métodos da classe AlunoDAO no qual estou trabalhando no momento:
[code]public List getLista(){
try{
List alunos = new ArrayList();
PreparedStatement stmt = this.connection.prepareStatement(“select * from tb_aluno”);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Aluno aluno = new Aluno();
aluno.setNome(rs.getString("nome"));
aluno.setCpf(rs.getString("cpf"));
aluno.setTelefone(rs.getString("telefone"));
aluno.setEndereco(rs.getString("endereco"));
aluno.setBairro(rs.getString("bairro"));
aluno.setCidade(rs.getString("cidade"));
aluno.setUf(rs.getString("estado"));
alunos.add(aluno);
}
rs.close();
stmt.close();
return alunos;
}catch(SQLException e){
throw new RuntimeException(e);
}
}
public void remove(Aluno aluno){
String sql = "delete from tb_aluno where id=? ";
try{
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setLong(1, aluno.getId());
stmt.execute();
}catch(SQLException e){
throw new RuntimeException();
}
}[/code]
Fiz as alterações sugeridas…minha classe AlunoDAO ficou da seguinte forma:
[code]public List getLista(){
try{
List alunos = new ArrayList();
PreparedStatement stmt = this.connection.prepareStatement(“select * from tb_aluno”);
ResultSet rs = stmt.executeQuery();
while(rs.next()){
Aluno aluno = new Aluno();
aluno.setId(rs.getLong("id"));
aluno.setNome(rs.getString("nome"));
aluno.setCpf(rs.getString("cpf"));
aluno.setTelefone(rs.getString("telefone"));
aluno.setEndereco(rs.getString("endereco"));
aluno.setBairro(rs.getString("bairro"));
aluno.setCidade(rs.getString("cidade"));
aluno.setUf(rs.getString("estado"));
alunos.add(aluno);
}
rs.close();
stmt.close();
return alunos;
}catch(SQLException e){
throw new RuntimeException(e);
}
}
public void remove(Aluno aluno){
String sql = "delete from tb_aluno where id=? ";
try{
PreparedStatement stmt = this.connection.prepareStatement(sql);
stmt.setLong(1, aluno.getId());
stmt.execute();
}catch(SQLException e){
throw new RuntimeException();
}
}[/code]