Pessoal estou tendando excluir um registro através de um DAO mas não esta dando certo, e tambem não da erro…
Nesse JSP eu listo os alunos em um combo para selecionar qual sera excluido:
<%
List <Aluno> alunos = AlunoDAO.recuperateAll();
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<form name="form" method="post" action="Excluindo.jsp">
<select name="cmbExclui">
<option>--- Selecione Aluno ---</option>
<%for(int i=0; i<alunos.size(); i++){%>
<option value="<%= alunos.get(i).getIdAluno() %>"><%= alunos.get(i).getNomeAluno() %></option>
<%}%>
</select>
<input type="submit" value="Excluir" />
</form>
</body>
</html>
então será “chamado” essa outra página onde eu recupero os dados:
<%
int cod;
cod = Integer.valueOf(request.getParameter("cmbExclui"));
Aluno aluno;
aluno = new Aluno();
aluno = AlunoDAO.recuperate(cod);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<h1>Excluir Aluno</h1>
<form id="form1" name="form1" method="post" action="SrvExcluiAluno">
<p> </p>
<table width="25%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="41%" height="20">Código</td>
<td width="59%"><input name="txtCod" type="text" value="<%= aluno.getIdAluno() %>" /></td>
</tr>
<tr>
<td width="41%" height="20">Nome</td>
<td width="59%"><input name="txtNome" type="text" value="<%= aluno.getNomeAluno() %>" /></td>
</tr>
<tr>
<td>Idade</td>
<td><input name="txtIdade" type="text" value="<%= aluno.getIdadeAluno() %>" /></td>
</tr>
<tr>
<td>Ano</td>
<td><input name="txtAno" type="text" value="<%= aluno.getAnoAluno() %>" /></td>
</tr>
<tr>
<td>Observação</td>
<td><input name="txtObs" type="text" value="<%= aluno.getObsAluno() %>" /></td>
</tr>
<tr>
<td>Média</td>
<td><input name="txtMedia" type="text" value="<%= aluno.getMediaAluno() %>" /></td>
</tr>
<tr>
<td>Fone</td>
<td><input name="txtFone" type="text" value="<%= aluno.getFoneAluno() %>" /></td>
</tr>
<tr>
<td><input name="Cadastrar" type="submit" id="Excluir" value="Excluir"></td>
<td> </td>
</tr>
</table>
</form>
</body>
</html>
Essa página por sua vez envia para o servlet o código do registro que será Excluido, olha o servlet:
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int cod;
AlunoDAO aluno = new AlunoDAO();
boolean result=false;
cod = Integer.valueOf(request.getParameter("txtCod"));
aluno.delete(cod);
result = true;
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
try {
out.println("<html>");
out.println("<head>");
out.println("<title>Resultado da Inclusão</title>");
out.println("</head>");
out.println("<body>");
if (result) {
out.println("<h1>Excluido com sucesso!</h1>");
} else {
out.println("<h1>Erro ao Salvar Aluno</h1>");
}
out.println("<a href='index.jsp'>VOLTAR</a>");
out.println("</body>");
out.println("</html>");
} finally {
out.close();
}
}
// <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
/**
* Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP <code>POST</code> method.
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
e por final o delete no DAO:
public boolean delete(int alu) {
String sql = "DELETE aluno from aluno WHERE idAaluno = ?" + alu;
Aluno aluno;
aluno = new Aluno();
try {
PreparedStatement pstm = (PreparedStatement) new Conexao().getConexao().prepareStatement(sql);
pstm.setInt(1, aluno.getIdAluno());
pstm.executeUpdate();
System.out.println("Excluido com sucesso");
return true;
} catch (Exception e) {
return false;
}
}
Simplesmente não funciona…e tambem não aparece nada no log do tomcat…Obrigado.