I’m having trouble persisting the data in the BD

Asked

Viewed 102 times

1

I am connected to the postgresql database, I put the postgresql-9.4-1201.jdbc4.jar driver. When I click to register this window appears this is not listing or persisting. The problem is that no error message appears in the console. Someone’s been there to help me?

Estou utilizando só o method get para fazer requisições de formlário

The jsp page looks like this:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Cadastro do Aluno</title>
</head>
<body>
<form action="alunoCONTROLLER" method="get" >
    <input type="hidden" name="operacao" value="inserir" >

    <label for="nome">Nome:</label>
    <input type="text" id="nome" name="nome"/><br/>

    <label for="telefone">Telefone:</label>
    <input type="text" id="telefone" name="telefone"/><br/>

    <label for="email" >Email:</label>
    <input type="text" id="email" name="email"><br/>

    <label for="endereco">Endereço:</label>
    <input type="text" id="endereco" name="endereco"><br/>

    <label for="matricula">Matrícula:</label>
    <input type="text" id="matricula" name="matricula"><br/>

    <label for="idade">Idade:</label>
    <input type="text" id="idade" name="idade"><br/>

    <label for="ativo">Ativo:</label>
    <input type="checkbox" id="ativo" name="ativo"><br/>

    <input type="submit" value="Cadastre">
</form>

The controller is like this:

package CONTROLLER;

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 DAO.AlunoDAO;
import MODEL.Aluno;

@WebServlet("/alunoCONTROLLER")
public class AlunoCONTROLLER extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public AlunoCONTROLLER() {
    super();

}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    AlunoDAO dao = new AlunoDAO();
    Aluno aluno = new Aluno();

    String operacao = request.getParameter("operacao");

    switch(operacao){
    case "incluir":
        aluno.setNome(request.getParameter("nome"));
        aluno.setAtivo(Boolean.parseBoolean(request.getParameter("ativo")));
        aluno.setEmail(request.getParameter("email"));
        aluno.setEndereco(request.getParameter("endereco"));
        aluno.setIdade(Integer.parseInt(request.getParameter("idade")));
        aluno.setMatricula(Long.parseLong(request.getParameter("matricula")));
        aluno.setTelefone(Long.parseLong(request.getParameter("telefone")));
        dao.save(aluno);

        response.sendRedirect("listar.jsp");
        break;

    case "deleter":
        aluno.setId(Integer.parseInt(request.getParameter("id")));
        dao.delete(aluno);
        response.sendRedirect("listar.jsp");

        break;

    case "atualizar":

        aluno.setId(Integer.parseInt(request.getParameter("id")));
        aluno.setNome(request.getParameter("nome"));
        aluno.setEmail(request.getParameter("email"));
        aluno.setEndereco(request.getParameter("endereco"));
        aluno.setIdade(Integer.parseInt(request.getParameter("idade")));
        aluno.setMatricula(Long.parseLong(request.getParameter("matricula")));
        aluno.setTelefone(Long.parseLong(request.getParameter("telefone")));
        aluno.setAtivo(Boolean.parseBoolean(request.getParameter("ativo")));

        dao.update(aluno);
        response.sendRedirect("listar.jsp");
        break;  
    }
  }
}

And the list:

<%@page import="MODEL.Aluno"%>
<%@page import="DAO.AlunoDAO" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Lista de alunos</title>
</head>
<body>
    <table>
        <tr>
        <td>ID</td>
        <td>Nome</td>
        <td>Email</td>
        <td>Telefone</td>
        <td>Matricula</td>
        <td>Estado</td>
        <td>Endereco</td>
        <td>Idade</td>
        <td>Ação</td>
    </tr>
    <% 
        for(Aluno aluno : new AlunoDAO().list()){
    %>  
    <tr>
        <td><%=aluno.getId() %></td>
        <td><%=aluno.getNome()%></td>
        <td><%=aluno.getEmail() %></td>
        <td><%=aluno.getTelefone() %></td>
        <td><%=aluno.getMatricula() %></td>
            <%if(aluno.getAtivo() == true) {%>
            <td>Ativo</td>
              <%} else { %>
            <td>Desativado</td>
            <%} %>
        <td><%=aluno.getEndereco() %></td>
        <td><%=aluno.getIdade() %></td>
        <td><a href="alunoCONTROLLER?operacao=deletar&id=<%=aluno.getId()%>">Deleter</a></td>
        <td><a href="atualizar.jsp?id=<%=aluno.getId()%>">Editar</a></td>
  </tr>
  <%} %>
</table>
</body>
</html>

Hallucination

package DAO;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import MODEL.Aluno;

public class AlunoDAO implements IDAO{

private String sql;
private PreparedStatement pst;
private ResultSet rs;
private Connection connection;

public AlunoDAO(){
    if(connection == null){
        connection = new ConnectionFactory().getconnection();
    }
}

@Override
public void save(Aluno aluno) {
        sql = "insert into aluna(nome,"
            + " email,"
            + " matricula,"
            + " endereco,"
            + "telefone"
            + "idade,"
            + "ativo) "
            + "values(?,?,?,?,?,?,?)";

    try {

    pst = connection.prepareStatement(sql);

    pst.setString(1, aluno.getNome());
    pst.setString(2, aluno.getEmail());
    pst.setLong(3, aluno.getMatricula());
    pst.setString(4, aluno.getEndereco());
    pst.setLong(5, aluno.getTelefone());
    pst.setInt(6, aluno.getIdade());
    pst.setBoolean(7, aluno.getAtivo());

    pst.execute();
    pst.close();

    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

@Override
public void update(Aluno aluno) {
    sql = "update aluna set "
            + "nome=?,"
            + " telefone=?,"
            + " endereco=?, "
            + "idade=?, "
            + "email=?,"
            + "matricula=?,"
            + " ativo=? where id=?";

    try {

        pst = connection.prepareStatement(sql);

        pst.setString(1, aluno.getNome());
        pst.setString(2, aluno.getEmail());
        pst.setLong(3, aluno.getMatricula());
        pst.setString(4, aluno.getEndereco());
        pst.setLong(5, aluno.getTelefone());
        pst.setInt(6, aluno.getIdade());
        pst.setBoolean(7, aluno.getAtivo());
        pst.setInt(8, aluno.getId());

        pst.execute();
        pst.close();

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }


}

@Override
public void delete(Aluno aluno) {

    sql = "delete from aluna where id=?";

    try{
        pst = connection.prepareStatement(sql);

        pst.setInt(1, aluno.getId());

        pst.execute();
        pst.close();

    } catch (SQLException e){
        throw new RuntimeException(e);
    }

}

@Override
public Aluno find(Integer id) {
    Aluno aluno = null;
        sql = "select * from aluna where id=?";

    try {
        pst = connection.prepareStatement(sql);
        pst.setInt(1, id);

        rs = pst.executeQuery();

        while(rs.next()){
            aluno = new Aluno();

            aluno.setId(rs.getInt("id"));
            aluno.setNome(rs.getString("nome"));
            aluno.setAtivo(rs.getBoolean("ativo"));
            aluno.setEmail(rs.getString("email"));
            aluno.setEndereco(rs.getString("endereco"));
            aluno.setIdade(rs.getInt("idade"));
            aluno.setMatricula(rs.getLong("matricula"));
            aluno.setTelefone(rs.getLong("telefone"));
        }

        return aluno;

    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

@Override
public List<Aluno> list() {

    List<Aluno> alunos = new ArrayList<Aluno>();
    Aluno aluno;

sql = "select * from student";

    try {
        pst = connection.prepareStatement(sql);
        rs = pst.executeQuery();

        while(rs.next()){
            aluno = new Aluno();

            aluno.setId(rs.getInt("id"));
            aluno.setNome(rs.getString("nome"));
            aluno.setAtivo(rs.getBoolean("ativo"));
            aluno.setEmail(rs.getString("email"));
            aluno.setEndereco(rs.getString("endereco"));
            aluno.setIdade(rs.getInt("idade"));
            aluno.setMatricula(rs.getLong("matricula"));
            aluno.setTelefone(rs.getLong("telefone"));

            alunos.add(aluno);
        }

        return alunos;

    } catch(SQLException e) {
        throw new RuntimeException(e);
    }

}

}

  • In the application server log file appears something?

  • Nothing. What appears is that message Tomcat started...

  • 1

    You can debug your doGet method?

  • Ahn. Good idea! In ide, it is not?

  • 1

    Yes. Put a breakpoint and start the server in debug mode.

  • How is your class Alunodao?

  • 2

    Look at this here: JSP-> <input type="Hidden" name="operation" value="insert" > Controller-> switch(operation){ case "include" [...] In one is "insert" in the other "include""

  • I ran in debug mode and I saw here that it does not enter any Oo switch option

  • @Pedroteles That’s right! Thank you!!

  • @Pedroteles You have the opportunity to create the answer to this question according to your comment.

Show 5 more comments
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.