Search for NULL mysql via Java

Asked

Viewed 48 times

0

I intend to save a residence in my database via java when one of the searched parameters is NULL, but it turns out I can’t find a way to pass that command.

My code is:

public Usuario registraSaida(String nome) throws SQLException {
    String sql = "UPDATE usuario_acesso SET data_saida = ? WHERE usuario_nome = ? AND data_saida = NULL";
    java.sql.Timestamp date = new java.sql.Timestamp(new java.util.Date().getTime());

    PreparedStatement pst = conexao.prepareStatement(sql);
    pst.setTimestamp(1, date);
    pst.setString(2, nome);
    pst.executeUpdate();

    System.out.println(nome);
    return null;
}
  • 1

    In SQL, every comparison with NULL will return false. In case, you need to check if the date is null: data_saida IS NULL

1 answer

2


Change the completion of String sql for

String sql = "UPDATE usuario_acesso SET data_saida = ? WHERE usuario_nome = ? AND data_saida IS NULL";

As stated in the commentary, the "comparison" should be made with IS NULL.

Browser other questions tagged

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