Using java change method

Asked

Viewed 716 times

1

How I change the database using the method changes in a Testaaltera class. I use the model gets and sets to change the data? I need to use the Search(int id) method to return an object?

//pesquisa pelo id e retorna um contato
public Contato pesquisarId(int id) {

    try {
        String sql = "select * from contatos where id = " + id;

        Contato contato = new Contato();

        PreparedStatement stmt = this.connection.prepareStatement(sql);

        ResultSet rs = stmt.executeQuery();

        while(rs.next()) {
            contato.setId(rs.getLong("id"));
            contato.setNome(rs.getString("nome"));
            contato.setEmail(rs.getString("email"));
            contato.setEndereco(rs.getString("endereco"));

            Calendar data = Calendar.getInstance();
            data.setTime(rs.getDate("dataNascimento"));
            contato.setDataNascimento(data);

        }

        rs.close();
        stmt.close();
        return contato;

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

//altera
public void altera(Contato contato) {

    String sql = "update contatos set nome=?, email=? where id = 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().getTimeInMillis()));
         stmt.setLong(5, contato.getId());

         stmt.execute();
         stmt.close();
    } catch (SQLException e) {
        throw new RuntimeException(e);
    }
}

Summarizing how it would look the class alters?

    package br.com.caelum.jdbc.teste;

import br.com.caelum.jdbc.dao.ContatoDao;
import br.com.caelum.jdbc.modelo.Contato;

public class TesteAltera {

    public static void main(String[] args) {

        ContatoDao dao = new ContatoDao();

        Contato contato = dao.pesquisarId(1);


        dao.altera(contato);

    }

}
  • 1

    For it to persist new data in the update command, you simply have to assign new values to the 'contact', no? contato.setNome("Fulano"); etc....

  • AEE was successful

  • 1

    Gabriel, I will answer your question so that the question can be closed. Note: Do not set a new id for the record you searched from the table. If possible, please evaluate my response.

  • @Gabrielfaria does not need you to put as title that was SOLVED your problem.

2 answers

2

The class should look something like this:

public class TesteAltera {

public static void main(String[] args) {

    ContatoDao dao = new ContatoDao();

    Contato contato = dao.pesquisarId(1);

    contato.setNome("Ciclano");
    contato.setEmail("[email protected]");
    contato.setEndereco("Av. das Nações, 147");
    Calendar novaData = Calendar.getInstance();

    contato.setDataNascimento(novaData);

    dao.altera(contato);

}

}

In order for you to change a database record within a Java program, you first need to retrieve it and place its values within an object. In this case, contato.

After this, you change its values to the desired ones and finally, have the object be persisted by the method altera() so that the database data is changed according to the data that is in the object.

  • Wow, I tidied up here. Really this solution is better. Thank you

-1

The class was like this:

    package br.com.caelum.jdbc.teste;

import java.util.Calendar;

import br.com.caelum.jdbc.dao.ContatoDao;
import br.com.caelum.jdbc.modelo.Contato;

public class TesteAltera {

    public static void main(String[] args) {

        ContatoDao dao = new ContatoDao();

        Contato contato = new Contato();

        long id = 1;

        contato.setId(id);
        contato.setNome("Nathalia");
        contato.setEmail("[email protected]");
        contato.setEndereco("Av. Paula Ferreira");
        Calendar novaData = Calendar.getInstance();

        contato.setDataNascimento(novaData);

        dao.altera(contato);

    }

}
  • Could you explain how it works? It is a subject that interests me, grateful. Note: put the explanation inside the answer, edit it.

  • @Guilhermenascimento In order for you to change a database record within a Java program, you first need to retrieve it and put its values inside an object. In this case, contato. After this, you change its values to the desired ones and finally, have the object be persisted by the method altera() so that the database data is changed according to the data that is in the object.

  • Note: put the explanation inside the answer, edit it.

  • @Guilhermenascimento If this subject interests you, I recommend you take a look at the Caelum handbooks, I’m studying there. These exercises are from the Java workbook for Web Development.

  • @Gabrielfaria I think you do not understand the focus of the site, here we seek to provide detailed answers in order to help those who have the current doubt as future visitors, this can still guarantee you some upvotes, I recommend you explain the code, no matter how brief an explanation ;)

Browser other questions tagged

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