Share via


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

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 smalldatetime SQL 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 time SQL Server data type.

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

See Also

Other Resources

Understanding the JDBC Driver Data Types