Configuring How java.sql.Time Values are Sent to the Server

Configuring How java.sql.Time Values are Sent to the Server

 

DownloadDownload JDBC Driver

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 SQL Server time type or as a datetime type.

This scenario applies when using one of the following methods:

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 SQL Server earlier than SQL Server 2008 do not support the time data type, so applications using java.sql.Time typically store java.sql.Time values either as datetime or smalldatetime SQL Server data types.

If you want to use the datetime and smalldatetimeSQL Server 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 SQL Server data type when working with java.sql.Time values, you should set the sendTimeAsDatetime connection property to false.

Be aware that when 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 SQL Server, see Using Date and Time Data.

In SQL Server 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 timeSQL Server data type.

  • Use java.sql.Timestamp when working with the datetime, smalldatetime, and datetime2SQL Server data types.

Note that sendTimeAsDatetime must be false for encrypted columns as encrypted columns do not 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)

Understanding the JDBC Driver Data Types

Community Additions

ADD
Show:
© 2016 Microsoft