oracle 我应该使用 JDBC getNString() 而不是 getString() 吗?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5892163/
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
Should I be using JDBC getNString() instead of getString()?
提问by Gnat
We are building a Java application backed by an Oracle database that we access using JDBC (drivers ojdbc6.jar
and orai18n.jar
). The database schema stores text columns primarily using the NVARCHAR2
data type.
我们正在构建一个由我们使用 JDBC(驱动程序ojdbc6.jar
和orai18n.jar
)访问的 Oracle 数据库支持的 Java 应用程序。数据库架构主要使用NVARCHAR2
数据类型存储文本列。
The JDBC documentationfor the JDBC ResultSet
says that getNString()
is particularly intended for use with the NCHAR, NVARCHAR etc. data types, but at the moment we are only using getString()
.
该JDBC文档的JDBCResultSet
说,getNString()
尤其用于与NCHAR,NVARCHAR等数据类型使用,但目前我们只使用getString()
。
This seems to work fine, so I am wondering why I should use getNString()
rather than getString()
. Is getString()
going to start failing if non-ASCII characters are entered, or is the Oracle JDBC driver indifferent as to which method I should use?
这似乎工作正常,所以我想知道为什么我应该使用getNString()
而不是getString()
. getString()
如果输入非 ASCII 字符,是否会开始失败,或者 Oracle JDBC 驱动程序是否对我应该使用哪种方法无动于衷?
EDIT: Seems that it may be database-dependent: SQL Server doesn't seem to mind which you use, depending on the connection parameters. Does anyone have any specific information on Oracle?
编辑:似乎它可能依赖于数据库:SQL Server 似乎并不介意您使用哪个,具体取决于连接参数。有没有人有关于 Oracle 的任何具体信息?
采纳答案by Gnat
I have done a test on our application and it seems that getNString()
is unnecessary with Java 6, JDBC 6, Oracle JDBC 6 drivers and Oracle 11.1.0.6.0. The test string I used was "Δ, Й, ?, ? ?, ?, あ, 叶, 葉, and ?", copied from http://en.wikipedia.org/wiki/Unicode.
我已经对我们的应用程序进行了测试,getNString()
对于 Java 6、JDBC 6、Oracle JDBC 6 驱动程序和 Oracle 11.1.0.6.0似乎没有必要。我使用的测试字符串是 "Δ, Й, ?, ? ?, ?, あ, 叶, 叶, 和 ?", 复制自http://en.wikipedia.org/wiki/Unicode。
Most of our data access is done via stored procedures. Java was able to set and retrieve the above test string correctly via setObject()
and getString()
(not setString()
for abstraction reasons), collecting data from the interface and writing it back to the interface as expected.
我们的大部分数据访问都是通过存储过程完成的。Java 能够通过setObject()
and getString()
(不是setString()
出于抽象原因)正确设置和检索上述测试字符串,从接口收集数据并将其按预期写回接口。
Hence getString()
works ok for Unicode data with Oracle 11g (like SQL Server as in the above link) so we will continue to use this rather than getNString()
.
因此getString()
适用于 Oracle 11g 的 Unicode 数据(如上面链接中的 SQL Server),因此我们将继续使用它而不是getNString()
.
回答by qwerty
If your DB uses the NVARCHAR2
datatype, it is designed to be storing multilingual data. Your program will break if any unicode data is stored in those columns. If I were you, I would move over to the getNXXX()
methods
如果您的数据库使用该NVARCHAR2
数据类型,则它旨在存储多语言数据。如果任何 unicode 数据存储在这些列中,您的程序就会中断。如果我是你,我会转向getNXXX()
方法