Check whether the field is null in java and mysql

Asked

Viewed 790 times

3

How do I check if a database field is filled or null? Do you have a Resultset method that does this check?

In this case it is a date field.

PreparedStatement ps = connection.prepareStatement("SELECT * FROM tarefas");
        ResultSet result = ps.executeQuery();
        while(result.next()) {
            Tarefa tarefa = new Tarefa();
            tarefa.setId(result.getLong(1));
            tarefa.setDescricao(result.getString(2));
            tarefa.setFinalizado(result.getBoolean(3));

            if(quero fazer a verificação aqui) {
                Calendar calendario = Calendar.getInstance();
                calendario.setTime(result.getDate(4));//Ainda não sei se isso funciona
                tarefa.setDataFinalizacao(calendario);
            }
            lista.add(tarefa);
        }

1 answer

5


In java use:

String descricao = result.getString(2);

if (descricao == null) {
     //Nulo
} else {
     //Não nulo
}

If you want to ignore rows containing a null column in mysql you can use IS NOT NULL thus:

SELECT * FROM tabela WHERE campo IS NOT NULL
  • 1

    I don’t know what I did wrong that I think I tried the first one, and it didn’t work out, now with your help it was.

  • 1

    @Welsonteles can sometimes be that it confused something or had not recompiled, but how good that solved.

  • as to the second solution would not suit me, since I want to take all the lines, just could not treat the null fields, Valew

Browser other questions tagged

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