Table Update in PostGreSQL

5

I have the following question in Java: how can I pass to SQL that it should capture the "date and time" of the computer and update the User Table in PostGreSQL?

Follow the code below:

public class UsuarioAtualizar {

    private Connection con = ConectarDB.getConexao();
    private dao.UsuarioD daoUsuario = new dao.UsuarioD();

    public void updateClienteByCpf(model.UsuarioM usuario){

        // Variáveis
        PreparedStatement ps = null;
        String sql = "update usuario set timestamp=?";

        // Inserção
        try {
            ps = con.prepareStatement(sql);
            //ps.setString(1, usuario.getNome());
            ps.executeUpdate();

        } catch (SQLException ex) {
           ex.printStackTrace();
        }
    }

    public dao.UsuarioD getDaoUsuario() {
        return daoUsuario;
    }

    public void setDaoUsuario(dao.UsuarioD daoUsuario) {
        this.daoUsuario = daoUsuario;
    }

}
    
asked by anonymous 29.11.2015 / 15:18

1 answer

1

Inside the JFrame containing the access I created the following.

                // Capturar Data e Hora do Acesso
                java.util.Date date = new java.util.Date();
                model.UsuarioA usuarioA = new model.UsuarioA(date);

                // Atualizar Tabela do Usuário com Ultimo Acesso
                UsuarioA uAtualizar = new UsuarioA();
                uAtualizar.updateUsuario(usuarioA);

I created a userA class in the Model package

public class UsuarioA {

public Date acesso;

// Getters & Setters
public Date getAcesso() {
    return acesso;
}
public void setAcesso(Date date) {
    this.acesso = date;
}
public UsuarioA(Date acesso) {
    this.acesso = acesso;
}   

}

Then I created the UserA class in the Dao package.

public class UsuarioA {

private Connection con = ConectarDB.getConexao();
private dao.UsuarioD daoUsuario = new dao.UsuarioD();

public void updateUsuario(model.UsuarioA usuario){

    // Variáveis
    PreparedStatement ps = null;
    String sql = "update usuario set acesso=?";

    // Inserção
    try {
        ps = con.prepareStatement(sql);
        ps.setDate(1, new Date(usuario.getAcesso().getTime()));
        ps.executeUpdate();

    } catch (SQLException ex) {
       ex.printStackTrace();
    }
}

public dao.UsuarioD getDaoUsuario() {
    return daoUsuario;
}

public void setDaoUsuario(dao.UsuarioD daoUsuario) {
    this.daoUsuario = daoUsuario;
}

}

    
29.11.2015 / 16:29