Erro no update

2 respostas
Jrmanzini

eu de novo hhh, ta foda, meu netbeans não ta debugando direito, oq esta errado com o update

aqui listo todos os clientes em um combo:

<%
    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="Alterando.jsp">
            <select name="cmbAluno">
                <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="Alterar" />
        </form>
    </body>
</html>

Depois é feita a chamada para a página que mostra os campos para que possam ser alterados:

<%
int cod;
cod = Integer.valueOf(request.getParameter("cmbAluno"));
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>Alteração de Aluno</h1>
<form id="form1" name="form1" method="post" action="SrvAleraAluno">
  <p>&nbsp;</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="Cadastrar" value="Cadastrar"></td>
      <td>&nbsp;</td>
    </tr>
  </table>
</form>
</body>
</html>

esta mostrando tudo certinho, esse é o servlet:

public class SrvAleraAluno extends HttpServlet {

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String  nome, ano, obs, fone, idade, media;
        int cod;

        cod = Integer.valueOf(request.getParameter("txtCod"));
        nome = request.getParameter("txtNome");
        idade = request.getParameter("txtIdade");
        ano = request.getParameter("txtAno");
        obs = request.getParameter("txtObs");
        media = request.getParameter("txtMedia");
        fone = request.getParameter("txtFone");

        Aluno alu;
        alu = new Aluno();
        alu.setIdAluno(Integer.valueOf(cod));
        alu.setNomeAluno(nome);
        alu.setIdadeAluno(Integer.valueOf(idade));
        alu.setAnoAluno(ano);
        alu.setObsAluno(obs);
        alu.setMediaAluno(Float.valueOf(media));
        alu.setFoneAluno(fone);

        boolean result = false;

        result = AlunoDAO.update(alu);

        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>Salvo 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 esse o DAO:

public class AlunoDAO {

    public static boolean save(Aluno aluno) {

        String sql = "insert into aluno (nomeAluno,anoAluno,idadeAluno,obsAluno,mediaAluno,foneAluno)values(?,?,?,?,?,?)";
        try {
            PreparedStatement pstm = (PreparedStatement) new Conexao().getConexao().prepareStatement(sql);
            pstm.setString(1, aluno.getNomeAluno());
            pstm.setString(2, aluno.getAnoAluno());
            pstm.setInt(3, aluno.getIdadeAluno());
            pstm.setString(4, aluno.getObsAluno());
            pstm.setFloat(5, aluno.getMediaAluno());
            pstm.setString(6, aluno.getFoneAluno());

            pstm.executeUpdate();
            return true;

        } catch (Exception e) {
            System.out.println("Erro ao inserir o Aluno\n" + e);// é impressa essa linha.
            return false;
        }
    }

     

    public static boolean update(Aluno aluno) {
        String sql = "UPDATE aluno SET idAluno = ?, nomeAluno = ?, anoAluno = ?, idadeAluno= ?, obsAluno = ?, mediaAluno = ? , foneAluno =? WHERE idAluno = ?";
        try {
            PreparedStatement pstm = (PreparedStatement) new Conexao().getConexao().prepareStatement(sql);
            pstm.setInt(1, aluno.getIdAluno());
            pstm.setString(2, aluno.getNomeAluno());
            pstm.setString(3, aluno.getAnoAluno());
            pstm.setInt(4, aluno.getIdadeAluno());
            pstm.setString(5, aluno.getObsAluno());
            pstm.setFloat(6, aluno.getMediaAluno());
            pstm.setString(7, aluno.getFoneAluno());
            pstm.executeUpdate();
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static Aluno recuperate(int codigo) {
        Aluno alu;
        String sql = "SELECT idAluno,nomeAluno,anoAluno,idadeAluno,obsAluno,mediaAluno,foneAluno from aluno WHERE idAluno = " + codigo;
        try {
            Statement stm = new Conexao().getConexao().createStatement();
            ResultSet rs = stm.executeQuery(sql);
            if (rs.next()) {
                alu = new Aluno();
                alu.setIdAluno(rs.getInt("idAluno"));
                alu.setNomeAluno(rs.getString("nomeAluno"));
                alu.setAnoAluno(rs.getString("anoAluno"));
                alu.setIdadeAluno(rs.getInt("idadeAluno"));
                alu.setObsAluno(rs.getString("obsAluno"));
                alu.setMediaAluno(rs.getFloat("mediaAluno"));
                alu.setFoneAluno(rs.getString("foneAluno"));
                return alu;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    public static List <Aluno> recuperateAll() {
        List<Aluno> lista = new ArrayList<Aluno>();
        String sql;
        Aluno alu;
        
        sql = "SELECT idAluno, nomeAluno,anoAluno,idadeAluno,obsAluno,mediaAluno,foneAluno from aluno ORDER BY nomeAluno";
        try {
            Statement stm = (Statement) new Conexao().getConexao().createStatement();
            ResultSet rs = (ResultSet) stm.executeQuery(sql);
            while (rs.next()) {
                alu = new Aluno();
                alu.setIdAluno(Integer.valueOf(rs.getString("idAluno")));
                alu.setNomeAluno(rs.getString("nomeAluno"));
                alu.setAnoAluno(rs.getString("anoAluno"));
                alu.setIdadeAluno(Integer.valueOf(rs.getString("idadeAluno")));
                alu.setObsAluno(rs.getString("obsAluno"));
                alu.setMediaAluno(Float.parseFloat(rs.getString("mediaAluno")));
                alu.setFoneAluno(rs.getString("foneAluno"));
               lista.add(alu);
            }

        } catch (Exception e) {
            lista = null;
            System.out.println("Erro ao montar lista de Clientes\nErro:\n" + e);
        }
        return lista;
    }


}

Obrigado

2 Respostas

peerless

E qual é o erro?

Jrmanzini

No log do tomcat não aparece nada…
mas a mensagem mostrada é:

protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        String  nome, ano, obs, fone, idade, media;
        int cod;

        cod = Integer.valueOf(request.getParameter("txtCod"));
        nome = request.getParameter("txtNome");
        idade = request.getParameter("txtIdade");
        ano = request.getParameter("txtAno");
        obs = request.getParameter("txtObs");
        media = request.getParameter("txtMedia");
        fone = request.getParameter("txtFone");
       
        Aluno alu;
        alu = new Aluno();
        alu.setIdAluno(Integer.valueOf(cod));
        alu.setNomeAluno(nome);
        alu.setIdadeAluno(Integer.valueOf(idade));
        alu.setAnoAluno(ano);
        alu.setObsAluno(obs);
        alu.setMediaAluno(Float.valueOf(media));
        alu.setFoneAluno(fone);

        boolean result = false;
        result = AlunoDAO.update(alu);

        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>Alterado com Sucesso!</h1>");
            } else {
                out.println("<h1>Erro ao Alterar Aluno</h1>");//MOSTRA ESSA MENSAGEM!!!
            }
            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>
}

obrigado

Criado 5 de dezembro de 2010
Ultima resposta 6 de dez. de 2010
Respostas 2
Participantes 2