I cannot Update my Object after being recovered

Asked

Viewed 47 times

0

I am trying to update my form after recovering an object and I cannot.

My DAO

    public List<Contato> listar() {
    List<Contato> contatos = new ArrayList<>();
    String sql = "select * from contato";

    try (PreparedStatement stmt = connection.prepareStatement(sql)) {

        try (ResultSet rs = stmt.executeQuery()) {

            while (rs.next()) {
                Contato contato = buscaContato(rs);
                contatos.add(contato);
            }
        }
        return contatos;
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

public Contato buscaPeloID(Contato contato) {
    String sql = "select * from contato where id = ?";

    try (PreparedStatement stmt = this.connection.prepareStatement(sql)) {
        stmt.setInt(1, contato.getId());
        ResultSet resultSet = stmt.executeQuery();

        while (resultSet.next()) {
            return buscaContato(resultSet);
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return null;
}

private Contato buscaContato(ResultSet rs) throws SQLException {
    Contato contato = new Contato();
    contato.setId(rs.getInt("id"));
    contato.setNome(rs.getString("nome"));
    contato.setEmail(rs.getString("email"));
    contato.setEndereco(rs.getString("endereco"));
    Date date = rs.getDate("data_nascimento");
    if (date != null) {
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        contato.setDataNascimento(c);
    }
    return contato;
}

public void atualiza(Contato contato) {

    String sql = "update contato set nome=?, email=?, endereco=?, data_nascimento = ?  where id=?";

    try (PreparedStatement stmt = connection.prepareStatement(sql)) {
        stmt.setString(1, contato.getNome());
        stmt.setString(2, contato.getEmail());
        stmt.setString(3, contato.getEndereco());
        stmt.setDate(4, new Date(contato.getDataNascimento() != null ? contato.getDataNascimento().getTimeInMillis() : null));
        stmt.setInt(5, contato.getId());
        stmt.execute();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
public static void main(String[] args) throws SQLException {

    Connection c = ConnectionFactory.getConnection();
    ContatoDAO contatoDAO = new ContatoDAO(c);

    Contato cc = new Contato();
    cc.setId(26);

    Contato buscaPeloID = contatoDAO.buscaPeloID(cc);

    System.out.println("Recuperado: " +buscaPeloID.getNome());

    cc.setNome("Francisco Everaldo32");
    cc.setEndereco("Rua das Palmeiras 32");
    cc.setEmail("[email protected]");
    Calendar calendar = Calendar.getInstance();
    calendar.set(2018, 05, 9);
    cc.setDataNascimento(calendar);

    contatoDAO.atualiza(cc);

    System.out.println("Salvo");

}

Controller

public class AdicionaContatoLogica implements Logica {

@Override
public String executa(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Connection connection = ConnectionFactory.getConnection();

    String id = request.getParameter("id");
    String nome = request.getParameter("nome");
    String endereco = request.getParameter("endereco");
    String email = request.getParameter("email");
    String dataNascimento = request.getParameter("dataNascimento");

    Calendar calendar = Calendar.getInstance();
    try {
        Date data = new SimpleDateFormat("dd/MM/yyyy").parse(dataNascimento);
        calendar.setTime(data);
    } catch (ParseException e) {
        System.out.println("Erro ao converter Data: " + e);
        return "mvc?logica=ListaContatoLogica";
    }

    Contato contato = new Contato();

    contato.setId(Integer.parseInt(id));
    contato.setNome(nome);
    contato.setEndereco(endereco);
    contato.setEmail(email);
    contato.setDataNascimento(calendar);

    ContatoDAO dao = new ContatoDAO(connection);

    dao.atualiza(contato);

    return "mvc?logica=ListaContatoLogica";
}

View

<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%>

Contact Registration or Update

<form action="mvc?logica=AdicionaContatoLogica" method="post">
    ID<input type="text" name="id" value="${contato.id}"><br />
    Nome<input type="text" name="nome" value="${contato.nome}"><br />
    Email<input type="text" name="email" value="${contato.email}"><br />
    Endereco<input type="text" name="endereco" value="${contato.endereco}"><br />
    Data<input type="text" name="data" value="<fmt:formatDate value="${contato.dataNascimento.time}" pattern="dd/MM/yyyy" />">
    <input type="submit" value="Enviar">


</form>

  • Well, from what I saw in your code, there may be an error if the base is not accessible, or the id does not exist. Post the error message, please.

  • Hello, I made a small change to the most unsuccessful code! I tested by creating a main method inside the DAO and got it. when I update it displays null. and does not display my Stracktrace

No answers

Browser other questions tagged

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