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?
The presence of
getNString
andsetNString
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
orsetNString
: both will lead to the values being sent as unicode. This changes when the connection propertysendStringParametersAsUnicode
has been set tofalse
.See also NVARCHAR Support in Type 2 SQL Server 2008 JDBC Driver:
For more information on the
sendStringParametersAsUnicode
connection property, see Setting the Connection Properties.