Oracle、utf-8、NVARCHAR2,还有很多困惑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6258377/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 23:53:16  来源:igfitidea点击:

Oracle, utf-8, NVARCHAR2, and a lot of confusion

oraclespringutf-8nvarchar

提问by testing123

I have the following "translation" table in an oracle 10g database:

我在 oracle 10g 数据库中有以下“翻译”表:

ID  VARCHAR2(100 BYTE)
LANGUAGE    CHAR(2 BYTE)
COUNTRY CHAR(2 BYTE)
TRANSLATION NVARCHAR2(2000 CHAR)
TRACK_TIMESTAMP DATE
TRACK_USER  VARCHAR2(2000 BYTE)

When I try to do this:

当我尝试这样做时:

update translation set translation = '?' where id = 'MY_ID' And language = 'fr';

Then I run this:

然后我运行这个:

select * from translation where id = 'MY_ID' and language = 'fr';

and the translation column shows: Sinstead of ?and I have no idea why.

并且翻译栏显示:S而不是,?我不知道为什么。

Due to legacy issues I cannot convert the whole database to use UTF-8, are there any other options?

由于遗留问题,我无法将整个数据库转换为使用 UTF-8,还有其他选择吗?

Currently the national character set is AL16UTF16. The ordinary character set is WE8ISO8859P1.

目前国家字符集是 AL16UTF16。普通字符集是 WE8ISO8859P1。

I am currently using java 1.6

我目前使用的是 java 1.6

The above is a simplified example. Here is what the query looks like in my actual application:

以上是一个简化的例子。以下是我的实际应用程序中查询的样子:

UPDATE TRANSLATION SET TRANSLATION=? WHERE TRANSLATION.COUNTRY=? and TRANSLATION.ID=? and TRANSLATION.LANGUAGE=? 1=1,800 - 2,500 ?ufs par heure 2=CA 3=3_XT_FE_ECS18 4=fr

The problem here is instead of adding ?ufsit adds ?ufs

这里的问题是而不是添加?ufs它添加?ufs

采纳答案by Justin Cave

Since you are using bind variables rather than hard-coded literals, you should be able to pass Unicode strings to your UPDATE statement.

由于您使用的是绑定变量而不是硬编码文字,因此您应该能够将 Unicode 字符串传递给您的 UPDATE 语句。

If you were using straight JDBC to write to the database, there is an example in the JDBC Developer's Guide on writing data to a NVARCHAR2 column. If you are using a 1.5 JVM, it is necessary to use the OraclePreparedStatement.setFormOfUse call for each NVARCHAR2 column. In a 1.6 JVM, life gets easier because JDBC 4.0 added NCHAR and NVARCHAR2 types. If you are using a 1.5 JVM, getting an ORM framework like Spring to use the Oracle extensions to JDBC may be a non-trivial undertaking. I'm not familiar enough with Spring to know what steps would be necessary for that to happen.

如果您使用直接 JDBC 写入数据库,则 JDBC 开发人员指南中有一个关于将数据写入 NVARCHAR2 列的示例。如果您使用的是 1.5 JVM,则必须对每个 NVARCHAR2 列使用 OraclePreparedStatement.setFormOfUse 调用。在 1.6 JVM 中,生活变得更轻松,因为 JDBC 4.0 添加了 NCHAR 和 NVARCHAR2 类型。如果您使用的是 1.5 JVM,那么获得一个像 Spring 这样的 ORM 框架来使用 Oracle 对 JDBC 的扩展可能不是一件容易的事。我对 Spring 不够熟悉,不知道需要哪些步骤才能做到这一点。

Potentially, you may be able to modify the connection string to specify defaultNChar=true. That will force the driver to treat all character columns using the national character set. That may be enough to resolve your problem without getting Spring to use the OraclePreparedStatement extensions.

您可能可以修改连接字符串以指定 defaultNChar=true。这将强制驱动程序使用国家字符集处理所有字符列。这可能足以解决您的问题,而无需让 Spring 使用 OraclePreparedStatement 扩展。