Understanding Data Type Differences
There are a number of differences between the Java programming language data types and SQL Server data types. The Microsoft JDBC Driver for SQL Server helps to facilitate those differences through various types of conversions.
The JDBC character string data types are CHAR, VARCHAR, and LONGVARCHAR. The JDBC driver provides support for the JDBC 4.0 API. In the JDBC 4.0, the JDBC character string data types can also be NCHAR, NVARCHAR, and LONGNVARCHAR. These new character string types maintain Java native character types in Unicode format and remove the need to perform any ANSI-to-Unicode or Unicode-to-ANSI conversion.
|Fixed-length||The SQL Serverchar and nchar data types map directly to the JDBC CHAR and NCHAR types. These are fixed-length types with padding provided by the server in the case where the column has SET ANSI_PADDING ON. Padding is always turned on for nchar, but for char, in the case where the server char columns are not padded, the JDBC driver adds the padding.|
|Variable-length||The SQL Servervarchar and nvarchar types map directly to the JDBC VARCHAR and NVARCHAR types, respectively.|
|Long||The SQL Servertext and ntext types map to the JDBC LONGVARCHAR and LONGNVARCHAR type, respectively. These are deprecated types beginning in SQL Server 2005, so you should use large value types, varchar(max) or nvarchar(max), instead.|
Using the update<Numeric Type> and updateObject (int, java.lang.Object) methods will fail against text and ntext server columns. However, using the setObject method with a specified character conversion type is supported against text and ntext server columns.
The JDBC binary-string types are BINARY, VARBINARY, and LONGVARBINARY.
|Fixed-length||The SQL Serverbinary type maps directly to the JDBC BINARY type. This is a fixed-length type with padding provided by the server in the case where the column has SET ANSI_PADDING ON. When the server char columns are not padded, the JDBC driver adds the padding.|
The SQL Servertimestamp type is a JDBC BINARY type with the fixed length of 8 bytes.
|Variable-length||The SQL Servervarbinary type maps to the JDBC VARBINARY type.|
The udt type in SQL Server maps to JDBC as a VARBINARY type.
|Long||The SQL Serverimage type maps to the JDBC LONGVARBINARY type. This type is deprecated beginning in SQL Server 2005, so you should use a large value type, varbinary(max) instead.|
The JDBC exact numeric types map directly to their corresponding SQL Server types.
|BIT||The JDBC BIT type represents a single bit that can be 0 or 1. This maps to a SQL Serverbit type.|
|TINYINT||The JDBC TINYINT type represents a single byte. This maps to a SQL Servertinyint type.|
|SMALLINT||The JDBC SMALLINT type represents a signed 16-bit integer. This maps to a SQL Serversmallint type.|
|INTEGER||The JDBC INTEGER type represents a signed 32-bit integer. This maps to a SQL Serverint type.|
|BIGINT||The JDBC BIGINT type represents a signed 64-bit integer. This maps to a SQL Serverbigint type.|
|NUMERIC||The JDBC NUMERIC type represents a fixed-precision decimal value that holds values of identical precision. The NUMERIC type maps to the SQL Server numeric type.|
|DECIMAL||The JDBC DECIMAL type represents a fixed-precision decimal value that holds values of at least the specified precision. The DECIMAL type maps to the SQL Server decimal type.|
The JDBC DECIMAL type also maps to the SQL Servermoney and smallmoney types, which are specific fixed-precision decimal types that are stored in 8 and 4 bytes, respectively.
The JDBC approximate numeric types are REAL, DOUBLE, and FLOAT.
|REAL||The JDBC REAL type has seven digits of precision (single precision) and maps directly to the SQL Server real type.|
|DOUBLE||The JDBC DOUBLE type has 15 digits of precision (double precision) and maps to the SQL Serverfloat type. The JDBC FLOAT type is a synonym of DOUBLE. Because there can be confusion between FLOAT and DOUBLE, DOUBLE is preferred.|
The JDBC TIMESTAMP type maps to the SQL Server datetime and smalldatetime types. The datetime type is stored in two 4-byte integers. The smalldatetime type holds the same information (date and time), but with less accuracy, in two 2-byte small integers.
The custom type mapping feature of JDBC that uses the SQLData interfaces for the JDBC advanced types (UDTs, Struct, and so on). is not implemented in the JDBC driver.