java - 无法将阿拉伯字符插入oracle数据库

标签 java oracle jdbc character-encoding arabic

我在使用 JDBC 驱动程序从 java 中的 oracle 读取阿拉伯字符时遇到问题,主要问题是我找不到正确的字符编码来获取正确的数据,但我使用此方法手动解决了问题:

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();
}

此方法为我提供了正确的字符,如数据库中显示的字符,但是当我尝试更新/插入阿拉伯语数据时,它会保存错误的字符。 例如:我的文本在数据库中保存为“?????????”而不是“� � 巴 ” 。

这是我连接oracle数据库的方式。

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);

请帮我解决这个问题? 谢谢。

新注释:

数据库本身不使用UTF16字符集,但是

"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 命令:-

从 v$nls_parameters 中选择 *,其中参数 = 'NLS_CHARACTERSET';

结果应该是

参数

NLS_CHARACTERSET

AR8MSWIN1256

如果不是,您必须使用以下命令更改此参数的值:-

  • 按键盘上的 WINDOWS KEY + r

  • 写入:- SQLPLUS sys as sysdba

  • 按 Enter 键,然后输入密码或直接按另一个 Enter

  • 发出以下命令:

  • 立即关闭

  • 启动限制

  • 更改数据库字符集INTERNAL_USE AR8MSWIN1256;

  • 更改数据库字符集 AR8MSWIN1256;

  • 立即关闭

  • 启动

  • 将 NLS_LANG 注册表字符串的值更改为 AMERICAN_AMERICA.AR8MSWIN1256

    如果您的操作系统是 UNIX 风格,请使用

AR8ISO8859P6 而不是 AR8MSWIN1256 作为 NLS_CHARACTERSET 的值

  • 不要在数据库中使用国家数据类型(即 NVARCHAR、NTEXT 或 NCLOB),除非您要在数据库中使用除(阿拉伯语和英语)之外的其他语言

  • AR8MSWIN1256 字符集足以在同一字段中混合阿拉伯语和英语(据我所知)。

取自

关于java - 无法将阿拉伯字符插入oracle数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23610462/

相关文章:

java - 使用可变数量的参数委托(delegate)方法调用

oracle - 如何恢复Oracle表中的数据?

java - 排除 Oracle 故障 - 挂起的进程

jdbc - Logstash jdbc 连接器基于时间的数据

java - 搜索查询性能?

JAVA MySQL连接ClassNotFound异常

java - 如何访问字符串数组?(Android)

java.lang.NoSuchMethodError : org. eclipse.swt.internal.win32.OS.GetForegroundWindow() 错误

java - Kestrel 指南/教程/文档?

Oracle SQL*Loader 不支持 skip_index_maintenance 参数