SQL Server Nvarchar和Java预准备语句 [英] SQL Server Nvarchar and Java prepared statement
问题描述
我对向SQL Server 2008 R2插入字符串有疑问.当我尝试插入带有一些国家字母的字符串时,会收到?"反而.
I have a question regarding inserting strings to SQL Server 2008 R2. When I try to insert strings with some national letters, I receive "?" instead.
我知道在字符串文字的开头添加N
将解决此问题,但是我正在使用JDBC准备的语句,如下所示:
I know that adding N
at the beginning of a string literal will fix this problem, but I'm using JDBC prepared statements, like this:
INSERT INTO MyTable(col1, col2) VALUES (?,?);
我的问题是:如何添加此"N"字母?我应该做这样的事情吗?
My question is: How I can add this "N" letter? Should I do something like this?
INSERT INTO MyTable(col1, col2) VALUES (N?,N?);
说实话,我不相信,因为这根本行不通.
To be honest I'm not convinced because this is not working at all.
推荐答案
基于@a_horse_with_no_name和@ gofr1的评论,有2种解决方案:
Based on comments written by @a_horse_with_no_name and @gofr1 there are 2 solutions:
-
更改
sendStringParametersAsUnicode
-这导致所有字符串都将被视为"N"个字符串(这是全局更改)
Change
sendStringParametersAsUnicode
- this cause that all string will be treated as "N" Strings (this is globally change)
使用PreparedStatement.setNString()
代替setString()
-这是本地"更改.
Use PreparedStatement.setNString()
instead of setString()
- and this is "local" change.
两者都在工作-可以更好地满足您的需求:)
Both are working - use which fits better to your needs :)
这篇关于SQL Server Nvarchar和Java预准备语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!