V
V
vbNoName2017-08-20 09:53:25
MySQL
vbNoName, 2017-08-20 09:53:25

How to handle empty DATETIM cell in mysql db in java?

There is a table in mysql database which has a DATETIME field. This field may be null. When I execute SELECT to this table and I receive the data in such a way that at null in this field the error takes off. How to handle null datetime field?
Date date = resultSet.getDate("date_birth")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
i_visseri, 2017-08-20
@i_visseri

Either check resultSet.getDate("date_birth") for null explicitly or use Optional from Java 8 if the project allows. Then everything will look like this:

Optional<Date> date = Optional.ofNullable(resultSet.getDate("date_birth");

V
vbNoName, 2017-08-20
@vbNoName

Thank you all for your help. The solution was next. It was necessary to add to the database connection string so that
it would take the form of something like this
then null DATE field values ​​will be converted to java null

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question