JDBC getNString()

夙愿已清 提交于 2019-12-13 18:12:46

问题


I'm working with a Java web application backed by a Microsoft SQL Server. I have used nvarchar columns and my plan is to support Unicode characters. So in my JDBC layer I have used getNString() method from the result set and it works fine. However just for curiosity I changed all the getNString() methods to normal getString() methods and it also works fine displaying Unicode characters correctly.

I found the similar observation from below question as well

Should I be using JDBC getNString() instead of getString()?

Do you guys have any idea about this?


回答1:


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.



来源:https://stackoverflow.com/questions/49920395/jdbc-getnstring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!