Use method changes java

1

How do I make change in the database using the method changes in a ClassAlertFoundation. Do I use the gets and sets of the template to change the data? Do I need to use the SearchId (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);
    }
}

In short how would the class change?

    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);

    }

}
    
asked by anonymous 17.08.2016 / 21:17

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);

}

}

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 your values to those you want, and finally, you have the object persisted by the altera() method so that the database's data is changed according to the data that is in the object.

    
17.08.2016 / 22:08
-1

The class looks 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);

    }

}
    
17.08.2016 / 21:59