[Resolvido]Ajuda com Servlet

3 respostas
U

Olá galera do GUJ,

Estou fazendo uma agenda eletronica simples, usando o html e servlet,
estou com um problema no codigo, apos debugar achei o erro mais não consigo corrigir,
quando executa a linha o sistema para, tipo da um break.

Essa é a classe:
a linha do erro é a 27

@Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);


        String cod = request.getParameter("codigo");
        String desc = request.getParameter("descricao");
        String opr = request.getParameter("operacao");

        response.setContentType("text/html;charset=UTF-8");

        PrintWriter out = response.getWriter();

        String html = "<html>";
        html += "<head>";
        html += "<title>Categoria</title>";
        html += "</head>";
        html += "<body>";

        if (opr.compareTo("Inserir") == 0) {
            if (desc == "" || desc == null && cod == null || cod == "") {
                int retorno = 0;
                CategoriaDAO catgDAO = new CategoriaDAO();
                Categoria catg = new Categoria();

                retorno = catgDAO.incluirCategoria(catg);


                if (retorno == 0) {
                    html += "Inclusão não efetuada";
                }

                if (retorno == 2) {
                    html += "Conta já existente";
                }

                if (retorno == 1) {
                    html += "Inclusão efetuada com sucesso";
                }

                html += "<input type=button onclick=javascript:history.back() value=Voltar>";


            } else {
                html += "Erro: Informe o nome da categoria ou deixe o codigo em branco!";
            }
            
            

        }

        html += "</body>";
        html += "</html>";
        out.println(html);

    }

Metodo da classe dao:

public int incluirCategoria(Categoria a) throws Exception {

        // retorna 0 => erro na inclusão
        // 1 => incluiu
        // 2 => nome cadastrado

        int retorno = 0;
        String sql = null;
        Connection conn = this.conexao.abreConexaoBD();
        Statement st = conn.createStatement();
        ResultSet rs;

        sql = "select * from categoria where descricao =" + a.getDescricao() + "";
        rs = st.executeQuery(sql);

        if (rs.next() == false) {
            retorno = 0;
        } else {
            retorno = 2;
        }
        if (retorno == 0) {

            sql = "insert into categoria (descricao)";
            sql += " values (";
            sql += "'" + a.getDescricao() + "');";

            int qRs = st.executeUpdate(sql);

            if (qRs == 0) {
                retorno = 0;

            } else {
                retorno = 1;
            }
        }
        st.close();
        conn.close();
        return retorno;
    }

Tela do sistema:

<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body>
        <h3>Cadastro de Categoria</h3>
        <form action="ServletCategoria" method="post">
            <table>
                <tr>
                    <td>Codigo</td>
                    <td><input id="codigo" name="codigo" type="text" />
                    </td>
                </tr>
                <tr>
                    <td>Nome</td>
                    <td><input id="descricao" name="descricao" type="text" />
                    </td>
                </tr>
            </table>
            <input type="submit" name="operacao" value="Inserir" /> 
            <input type="submit" name="operacao" value="Alterar" /> 
            <input type="submit" name="operacao" value="Excluir" /> 

            <input type="reset" value="Limpar" />
            <input type="button" onclick="javascript:history.back()" value="Voltar">
        </form>
    </body>
</html>

obs: não aparece nenhuma mensagem de erro na tela, apenas o sistema para sem passar pelas outras linhas.

3 Respostas

CristianPalmaSola10

ola, algum processo seu não esta ficando travado talvez seja o metodo que abre a conexao com o banco de dados, da uma verificada, se ele não consegue abrir a conexao leva um tempo ate lancar um timeout no sistema

carolino

debuga o método incluirCategoria da classe CaregoriaDAO também…

U

agora funciono, com algumas pesquisas aprofundada e varios debug dei conta de corrigir

veja como deve ficar:

try {
       retorno = catgDAO.incluirCategoria(catg);
} catch (Exception ex) {
       Logger.getLogger(ServletCategoria.class.getName()).log(Level.SEVERE, null, ex);
}
Criado 18 de setembro de 2012
Ultima resposta 18 de set. de 2012
Respostas 3
Participantes 3