JDBC getNString() [英] JDBC getNString()

查看:281
本文介绍了JDBC getNString()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用由Microsoft SQL Server支持的Java Web应用程序.我使用了nvarchar列,我的计划是支持Unicode字符.因此,在我的JDBC层中,我从结果集中使用了getNString()方法,并且效果很好.但是,出于好奇,我将所有getNString()方法都更改为普通的getString()方法,它也可以正常正确显示Unicode字符.

我从下面的问题中也发现了类似的观察结果

我应该使用JDBC getNString()而不是getString ()?

你们对此有任何想法吗?

我认为,getNStringsetNString的存在是JDBC的一个设计错误.但是,区分(VAR)CHAR和N(VAR)CHAR的数据库系统可以将此设置程序作为类型提示,以N(VAR)CHAR的特定格式发送数据.对于吸气剂来说,通常没有什么区别,因为在大多数驱动程序中,在调用此方法之前,数据已经被获取,并且驱动程序应该知道正确的转换.

特别是对于具有默认配置的Microsoft SQL Server JDBC驱动程序,使用setStringsetNString之间没有什么区别:两者都将导致值以unicode的形式发送.将连接属性sendStringParametersAsUnicode设置为false时,此更改.

另请参见

也就是说,使用JDBC 4 API处理NVARCHAR数据有一些好处 优点.主要优点是对NVARCHAR的正式认可 JDBC API的类型,包括访问器方法,例如 getNString/setNString/updateNString.例如,setNString 方法可用于将输入参数值发送到SQL Server中 Unicode,即使将sendStringParametersAsUnicode设置为"false".

以另一种方式输入,默认设置为 sendStringParametersAsUnicode = true,JDBC 4'N'方法的行为 关于NVARCHAR,与JDBC 3/JDBC 4非"N"方法相同的方法 数据.

有关sendStringParametersAsUnicode连接属性的更多信息,请参见Should I be using JDBC getNString() instead of getString()?

Do you guys have any idea about this?

解决方案

The presence of getNString and setNString is - in my opinion - a design mistake of JDBC. However database systems that discern between (VAR)CHAR and N(VAR)CHAR can take this setter as a type hint to send the data in their specific format for N(VAR)CHAR. For getters there will usually be no difference as in most drivers, the data will have already been fetched before this method can be called, and a driver should know the proper conversion.

Specifically for the Microsoft SQL Server JDBC driver with default configuration there is no difference between using setString or setNString: both will lead to the values being sent as unicode. This changes when the connection property sendStringParametersAsUnicode has been set to false.

See also 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.

For more information on the sendStringParametersAsUnicode connection property, see Setting the Connection Properties.

这篇关于JDBC getNString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆