无法将阿拉伯字符插入到Oracle数据库中 [英] Can't insert arabic characters into oracle database
问题描述
在使用JDBC驱动程序从java中的oracle中读取oracle阿拉伯字符时遇到问题,主要问题是我找不到正确的字符编码来获取正确的数据,但是我使用此方法手动解决了该问题:
I got problems while reading arabic characters from oracle in java using JDBC driver, the main problem was i couldn't find the proper character encoding to get the correct data , but i solved the problem manually using this method:
public static String cleanORCLString(String s) throws UnsupportedEncodingException {
byte[] bytes = s.getBytes("UTF16");
String x = new String(bytes, "Cp1256");
String finalS = x.substring(3);
StringBuilder sb = new StringBuilder(finalS);
for(int k = sb.length() - 1 ; k > 0 ; k--) {
if(!isEven(k)) {
sb.deleteCharAt(k);
}
}
return sb.toString();
}
此方法为我提供了正确的字符,如数据库中显示的那样,但是当我尝试更新/插入阿拉伯数据时,它将保存错误的字符. 例如:我的文本在数据库中另存为"?????????"而不是مرحبا".
this method give me the correct characters like its shown in database, but when I try to update/insert arabic data, it save wrong characters. For example: my text saved in database as "?????????" instead of "مرحبا".
这是我连接到oracle数据库的方式.
This is the way I connect to oracle database.
URL = ORCLConnProperties.ORCL_THIN_PREFIX + orclProp.getIpAddress()
+ orclProp.getPortNumber() + ORCLConnProperties.ORCL_THIN_SUFIX;
// URL = jdbc:oracle:thin:@10.0.0.12:1521:ORCL
System.out.println("URL: " + URL);
Properties connectionProps = new Properties();
connectionProps.put("characterEncoding", "Cp1256");
connectionProps.put("useUnicode", "true");
connectionProps.put("user", orclProp.getUserName());
connectionProps.put("password", orclProp.getPassword());
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException ex) {
System.out.println("Error: unable to load driver class!");
System.exit(1);
}
myDriver = new oracle.jdbc.driver.OracleDriver();
DriverManager.registerDriver(myDriver);
conn = DriverManager.getConnection(URL, connectionProps);
请帮助我解决这个问题? 谢谢.
please help me in solving this issue ? Thanks.
新注释:
数据库本身不使用UTF16字符集,但是
Database itself don't use UTF16 character set, but
" JDBC OCI驱动程序将数据从服务器传输到客户端 在数据库的字符集中.根据值 NLS_LANG环境变量,驱动程序处理字符集 转换:OCI将数据从数据库字符集转换为 UTF-8.然后,JDBC OCI驱动程序将UTF-8数据传递给JDBC. 类库,将UTF-8数据转换为UTF-16."
"the JDBC OCI driver transfers the data from the server to the client in the character set of the database. Depending on the value of the NLS_LANG environment variable, the driver handles character set conversions: OCI converts the data from the database character set to UTF-8. The JDBC OCI driver then passes the UTF-8 data to the JDBC Class Library, where the UTF-8 data is converted to UTF-16."
此注释在这里被提及: http://docs.oracle.com/cd/B10501_01/java .920/a96654/advanc.htm
this note is mentioned here: http://docs.oracle.com/cd/B10501_01/java.920/a96654/advanc.htm
推荐答案
首先,您可以使用以下命令检查数据库的NLS_CHARACTERSET参数 SQL * PLUS命令:-
First you may check the NLS_CHARACTERSET parameter of your database using the SQL*PLUS command :-
从v $ nls_parameters中选择*,其中参数='NLS_CHARACTERSET';
select * from v$nls_parameters where parameter = 'NLS_CHARACTERSET';
结果应该是
参数
VALUE
NLS_CHARACTERSET
NLS_CHARACTERSET
AR8MSWIN1256
AR8MSWIN1256
如果不是,则必须使用:-
if it's not, you have to change the value of this parameter using :-
-
在键盘上按WINDOWS KEY + r
hit WINDOWS KEY + r on your keyboard
写:-SQLPLUS sys作为sysdba
write :- SQLPLUS sys as sysdba
按Enter键,然后输入密码,或按另一个Enter键
press Enter then enter the password or just hit another Enter
发出以下命令:
立即关闭
启动限制
ALTER DATABASE CHARACTER SET INTERNAL_USE AR8MSWIN1256;
ALTER DATABASE CHARACTER SET INTERNAL_USE AR8MSWIN1256;
ALTER DATABASE CHARACTER SET AR8MSWIN1256;
ALTER DATABASE CHARACTER SET AR8MSWIN1256;
立即关闭
启动
将NLS_LANG注册表字符串的值更改为AMERICAN_AMERICA.AR8MSWIN1256
change the value of the NLS_LANG registry string into AMERICAN_AMERICA.AR8MSWIN1256
如果您的操作系统是UNIX风格 ,请使用
if your operating system is a flavor of UNIX use
AR8ISO8859P6 代替 AR8MSWIN1256 作为 NLS_CHARACTERSET
-
请勿在数据库中使用国家数据类型(即NVARCHAR,NTEXT或NCLOB),除非您打算在数据库中使用除(阿拉伯语和英语)之外的其他语言
DON'T use National datatypes (i.e NVARCHAR, NTEXT, or NCLOB ) in your database unless you are going to use other languages than (Arabic and English) inside your database
AR8MSWIN1256字符集足以在同一字段内混合阿拉伯语和英语(据我所知).
AR8MSWIN1256 character set is sufficient for mixing arabic and english inside the same field (as far as I know).
摘自