jTDS incorrectly reports data type info in result

2020-04-13 20:18发布

问题:

Currently, the jTDS JDBC driver (1.2.5) against Microsoft SQL Server 2008 appears to incorrectly report the data type for DATE columns as NVARCHAR.

It probably behaves the same for both earlier versions of jTDS and SQL Server (2005, 2000)

  • Are there any workarounds for this that don't require switching to a different driver (for example Microsoft's own driver) or patching the jTDS driver?

  • Also I would like to avoid having to perform queries against the data dictionary (INFORMATION_SCHEMA.COLUMNS view, etc.) to look up the data type information (and possibly cross-referencing against the output of "exec sp_datatype_info" in order to retrieve the SQL data types)

A quick perusal of the jTDS outstanding bugs doesn't indicate whether this will be fixed or not.

An answer to this question: JDBC - JTDS bug ? For columns of type date and time(x) seems to indicate that this doesn't occur in SQL Server 2005.

Thanks in advance.

回答1:

I found a solution with the condition

metaData.getColumnType(columnNumber) == 12

if this condition is satisfied, execute the query

SELECT System_Type_Id FROM Sys.Columns WHERE Name = [column name] AND Object_Id = (SELECT Object_Id FROM Sys.Tables WHERE Name = [table name])

for smalldatetime it will return 58

and for datetime it will return 61.