Check if the field is null in java and mysql

3

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

In 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);
        }
    
asked by anonymous 22.09.2017 / 00:40

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 that contain a null column in mysql you can use IS NOT NULL like this:

SELECT * FROM tabela WHERE campo IS NOT NULL
    
22.09.2017 / 00:50