Configuring how java.sql.Time values are sent
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:
- SQLServerCallableStatement.registerOutParameter(int, int)
- SQLServerCallableStatement.registerOutParameter(int, int, int)
- SQLServerCallableStatement.setTime
- SQLServerPreparedStatement.setTime
- SQLServerCallableStatement.setObject
- SQLServerPreparedStatement.setObject
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 SQL Server earlier than SQL Server 2008 (10.0.x) don't 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.
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.
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)