Skip to content

Latest commit

 

History

History
54 lines (37 loc) · 4.26 KB

configuring-how-java-sql-time-values-are-sent-to-the-server.md

File metadata and controls

54 lines (37 loc) · 4.26 KB
title description author ms.author ms.date ms.service ms.subservice ms.topic
Configuring how java.sql.Time values are sent
Learn how to configure how java.sql.Time values are sent to the server using the sendTimeAsDatetime connection option.
David-Engel
davidengel
08/12/2019
sql
connectivity
conceptual

Configuring how java.sql.Time values are sent

[!INCLUDEDriver_JDBC_Download]

If you use a java.sql.Time object or the java.sql.Types.TIME JDBC type to set a parameter, you can configure how the java.sql.Time value is sent to the server; either as a [!INCLUDEssNoVersion] time type or as a datetime type.

This scenario applies when using one of the following methods:

SendTimeAsDatetime

You can configure how the java.sql.Time value is sent by using the sendTimeAsDatetime connection property. For more information, see Setting the Connection Properties.

You can programmatically modify the value of the sendTimeAsDatetime connection property with SQLServerDataSource.setSendTimeAsDatetime.

Versions of [!INCLUDEssNoVersion] earlier than [!INCLUDEsql2008-md] don't support the time data type, so applications using java.sql.Time typically store java.sql.Time values either as datetime or smalldatetime [!INCLUDEssNoVersion] data types.

If you want to use the datetime and smalldatetime[!INCLUDEssNoVersion] data types when working with java.sql.Time values, you should set the sendTimeAsDatetime connection property to true. If you want to use the time [!INCLUDEssNoVersion] data type when working with java.sql.Time values, you should set the sendTimeAsDatetime connection property to false.

Sending java.sql.Time values into a parameter whose data type can also store the date, that default dates are different depending on whether the java.sql.Time value is sent as a datetime (1/1/1970) or time (1/1/1900) value. For more information about data conversions when sending data to a [!INCLUDEssNoVersion], see Using Date and Time Data.

In [!INCLUDEssNoVersion] JDBC Driver 3.0, sendTimeAsDatetime is true by default. In a future release, the sendTimeAsDatetime connection property may be set to false by default.

To ensure that your application continues to work as expected regardless of the default value of the sendTimeAsDatetime connection property, you can:

  • Use java.sql.Time when working with the time[!INCLUDEssNoVersion] data type.
  • Use java.sql.Timestamp when working with the datetime, smalldatetime, and datetime2[!INCLUDEssNoVersion] data types.

SendTimeAsDatetime must be false for encrypted columns as encrypted columns don't support the conversion from time to datetime. Beginning with Microsoft JDBC Driver 6.0 for SQL Server, the SQLServerConnection class has the following two methods to set/get the value of the sendTimeAsDatetime property.

  public boolean getSendTimeAsDatetime()
  public void setSendTimeAsDatetime(boolean sendTimeAsDateTimeValue)

See also

Understanding the JDBC driver data types