Search code examples
sql-serverjdbcunicodeutf-8nvarchar

JDBC getNString()


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?


Solution

  • 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.