I'm trying to use new java.time
classes with most recent version of Sql Server JDBC driver. As I read it should just work with methods: PreparedStatement.setObject()
and ResultSet.getObject()
.
So I created sample code, and can't get it work with ResultSets. I don't know what I'm doing wrong here.
Connection connection = DriverManager.getConnection(connectionString);
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM myTable WHERE ? BETWEEN date_from AND date_to");
preparedStatement.setObject(1, LocalDateTime.now()); // That works
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()) {
Object o = resultSet.getObject("date_from");
o.getClass() returns java.sql.Timestamp
LocalDateTime dateTime = resultSet.getObject("date_from", LocalDateTime.class);
}
This throws an exception:
com.microsoft.sqlserver.jdbc.SQLServerException: The conversion to class java.time.LocalDateTime is unsupported.
Driver version: mssql-jdbc-6.5.4.jre8-preview.jar
SQL Server version: 2016
How to interpret this sentence in table at bottom:
New Java classes in Java 8: LocalDate/LocalTime/LocalDateTime, OffsetTime/OffsetDateTime
New JDBC types: TIME_WITH_TIMEZONE, TIMESTAMP_WITH_TIMEZONE, REF_CURSOR
REF_CURSOR is not supported in SQL Server. Driver throws a SQLFeatureNotSupportedException exception if this type is used. The driver supports all other new Java and JDBC type mappings as specified in the JDBC 4.2 specification.
Object o = resultSet.getObject("date_from");
, what is the type of table-columndate_from
? – prasad_java.time
API with JDBC. – Arvind Kumar Avinash