I have a Java application that uses an Oracle database with date fields in a number of the tables.
I have a number of queries in the code in which attempt to retrieve the date values and stuff them into an instance variable.
These all worked fine when my Oracle JDBC driver was ojdbc8 version 19.8.0.0, but I've since had to upgrade to ojdbc17 version 23.7.0.25.01, and those queries now come back with the following error or something similar:
Could not extract column [1] from JDBC ResultSet [ORA-18716: {0} not in any time zone.DATE https://docs.oracle.com/error-help/db/ora-18716/] [n/a]
Here is an example of one of the queries in the code:
@PersistenceContext
private EntityManager em;
String sqlQueryString = "SELECT h.commentDate FROM CommentsHistory as h WHERE h.revisionId = 4878";
TypedQuery<Instant> resolvedCommentDateQuery = em.createQuery(sqlQueryString, Instant.class);
try {
//This is where the error is thrown!
List<Instant> instantList = resolvedCommentDateQuery.getResultList();
return null;
} catch (NoResultException nre) {
return null;
}
Previously, my pom.xml file dependency looked like this, and it worked fine:
<!-- Oracle JDBC Driver -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc8</artifactId>
<version>19.8.0.0</version>
</dependency>
I had to update my pom.xml file dependency to this, and now it throws the error:
<!-- Oracle JDBC Driver -->
<dependency>
<groupId>com.oracle.database.jdbc</groupId>
<artifactId>ojdbc17</artifactId>
<version>23.7.0.25.01</version>
</dependency>
ojdbc17
is strict about mismatches and will not auto convert Date
to Instant
which has caused ORA-18716 error. First of all, we will map oracle Date
to Java Date
and then we will convert Date
to Instant
TypedQuery<Date> resolvedCommentDateQuery = em.createQuery(sqlQueryString, Date.class);
try {
List<Date> dateList = resolvedCommentDateQuery.getResultList();
List<Instant> instantList = dateList.stream()
.map(Date::toInstant)
.collect(Collectors.toList());
return null;
} catch (NoResultException nre) {
return null;
}