Update Table in Postgresql

Asked

Viewed 124 times

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?

Follows code:

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

}
  • 1

    From the user’s computer? I think it is not possible, the database is installed on the server, there is no way he knows the time of the user’s pc. You should capture this via javascript and send it to your DAO, ai yes, save in sql.

  • 1

    What runs on the customer’s computer? Can’t send the date in this User object?

  • Done. User is on one pc and Server is on another. He wanted to be sent from the User to the Server his last access to register in the table. There is no way?

  • 2

    How, but is an executable running on the client? Is it a website? Written on what? Because what you want should be put on the client system, not the server. Edit the question and place the client code that makes the call to the server by passing this User object

  • It is a Java desktop application.

  • I solved it. I must answer my own question?

  • 2

    Absolutely! Your solution can help someone with the same problem as searching here :D.

  • Thank you all!

Show 3 more comments

1 answer

1


Inside the Jframe that contains 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 user 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 User 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;
}

}

Browser other questions tagged

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