无法在oracle数据库中插入阿拉伯字符 [英] Can't insert arabic characters into oracle database

查看:42
本文介绍了无法在oracle数据库中插入阿拉伯字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用 JDBC 驱动程序从 java 中的 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

推荐答案

首先你可以检查你的数据库的 NLS_CHARACTERSET 参数使用SQL*PLUS 命令:-

First you may check the NLS_CHARACTERSET parameter of your database using the SQL*PLUS command :-

select * from v$nls_parameters where parameter = 'NLS_CHARACTERSET';

select * from v$nls_parameters where parameter = 'NLS_CHARACTERSET';

结果应该是

参数

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 as sysdba

write :- SQLPLUS sys as sysdba

按回车键然后输入密码或直接按回车键

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;

更改数据库字符集 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).

取自

https://ksadba.wordpress.com/2008/06/10/how-to-show-arabic-characters-in-your-client-app/

这篇关于无法在oracle数据库中插入阿拉伯字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆