Skip to content
Advertisement

Inject Date or LocalDate into SQL query via jdbcTemplate.query?

I had a simple query like:

JavaScript

I injected a LocalDate into query:

JavaScript

Everything worked fine. After I moved my project with IDE to another PC, without any alterations, it started to throw exception:

JavaScript

I can use a workaround via converting LocalDate to Date:

JavaScript

but it does not seem right to me: I want to know the reason. What I checked so far:

  1. PostgreSQL version is the same (BTW tried upgrading to 14)
  2. JDK version is the same
  3. POM dependency for PostgreSQL is the same.

Advertisement

Answer

java.time.LocalDate has been available since Java-8 whereas your JDBC driver is based on Java-7, and hence the problem. Upgrade the JDBC driver to the latest one (following) or at least to the one supporting minimum Java-8.

JavaScript

Note: The java.util Date-Time API and its children are outdated and error-prone. It is recommended to stop using them completely and switch to the modern Date-Time API*.


* If you are working for an Android project and your Android API level is still not compliant with Java-8, check Java 8+ APIs available through desugaring. Note that Android 8.0 Oreo already provides support for java.time.

Advertisement