JDBC getNString()
JDBC getNString()
我正在使用由 Microsoft SQL 服务器支持的 Java Web 应用程序。我使用过 nvarchar 列,我的计划是支持 Unicode 字符。所以在我的 JDBC 层中,我使用了结果集中的 getNString()
方法并且它工作正常。然而,出于好奇,我将所有 getNString()
方法更改为普通 getString()
方法,它也可以正常显示 Unicode 字符。
我在下面的问题中也发现了类似的观察结果
Should I be using JDBC getNString() instead of getString()?
你们有什么想法吗?
在我看来,getNString
和 setNString
的存在是 JDBC 的设计错误。但是,区分 (VAR)CHAR 和 N(VAR)CHAR 的数据库系统可以将此 setter 作为类型提示,以 N(VAR)CHAR 的特定格式发送数据。对于 getter 通常与大多数驱动程序没有区别,在调用此方法之前已经获取了数据,并且驱动程序应该知道正确的转换。
专用于 Microsoft SQL Server JDBC 驱动程序,默认配置使用 setString
或 setNString
没有区别:两者都会导致值作为 unicode 发送。当连接 属性 sendStringParametersAsUnicode
设置为 false
.
时,这会发生变化
另见 NVARCHAR Support in Type 2 SQL Server 2008 JDBC Driver:
You do not need to use the JDBC 4 API to work with NVARCHAR (UCS-2)
data in SQL Server. Existing JDBC 3 APIs such as
getString/setString/updateString are used to get/set/update Unicode
values as Java Strings (which are always Unicode). The only thing to
be aware of when using the JDBC 3 methods is the setting of the
sendStringParametersAsUnicode property. This property is set to
'true' by default, which means that PreparedStatement and
CallableStatement parameters are sent to SQL Server in Unicode.
Changing the setting to 'false' allows the driver to save space in the
protocol by converting input parameter values to the character set of
the database when sending them.
That said, using the JDBC 4 API to work with NVARCHAR data has some
advantages. The main advantage is formal recognition of the NVARCHAR
type by the JDBC API, including accessor methods such as
getNString/setNString/updateNString. For example, the setNString
method can be used to send input parameter values to SQL Server in
Unicode, even if sendStringParametersAsUnicode is set to 'false'.
Put another way, with the default setting of
sendStringParametersAsUnicode=true, the JDBC 4 'N' methods behave the
same way as the JDBC 3/JDBC 4 non-'N' methods with respect to NVARCHAR
data.
有关 sendStringParametersAsUnicode
连接 属性 的详细信息,请参阅 Setting the Connection Properties。
我正在使用由 Microsoft SQL 服务器支持的 Java Web 应用程序。我使用过 nvarchar 列,我的计划是支持 Unicode 字符。所以在我的 JDBC 层中,我使用了结果集中的 getNString()
方法并且它工作正常。然而,出于好奇,我将所有 getNString()
方法更改为普通 getString()
方法,它也可以正常显示 Unicode 字符。
我在下面的问题中也发现了类似的观察结果
Should I be using JDBC getNString() instead of getString()?
你们有什么想法吗?
在我看来,getNString
和 setNString
的存在是 JDBC 的设计错误。但是,区分 (VAR)CHAR 和 N(VAR)CHAR 的数据库系统可以将此 setter 作为类型提示,以 N(VAR)CHAR 的特定格式发送数据。对于 getter 通常与大多数驱动程序没有区别,在调用此方法之前已经获取了数据,并且驱动程序应该知道正确的转换。
专用于 Microsoft SQL Server JDBC 驱动程序,默认配置使用 setString
或 setNString
没有区别:两者都会导致值作为 unicode 发送。当连接 属性 sendStringParametersAsUnicode
设置为 false
.
另见 NVARCHAR Support in Type 2 SQL Server 2008 JDBC Driver:
You do not need to use the JDBC 4 API to work with NVARCHAR (UCS-2) data in SQL Server. Existing JDBC 3 APIs such as getString/setString/updateString are used to get/set/update Unicode values as Java Strings (which are always Unicode). The only thing to be aware of when using the JDBC 3 methods is the setting of the sendStringParametersAsUnicode property. This property is set to 'true' by default, which means that PreparedStatement and CallableStatement parameters are sent to SQL Server in Unicode. Changing the setting to 'false' allows the driver to save space in the protocol by converting input parameter values to the character set of the database when sending them.
That said, using the JDBC 4 API to work with NVARCHAR data has some advantages. The main advantage is formal recognition of the NVARCHAR type by the JDBC API, including accessor methods such as getNString/setNString/updateNString. For example, the setNString method can be used to send input parameter values to SQL Server in Unicode, even if sendStringParametersAsUnicode is set to 'false'.
Put another way, with the default setting of sendStringParametersAsUnicode=true, the JDBC 4 'N' methods behave the same way as the JDBC 3/JDBC 4 non-'N' methods with respect to NVARCHAR data.
有关 sendStringParametersAsUnicode
连接 属性 的详细信息,请参阅 Setting the Connection Properties。