Reading Arabic text from Oracle database encoded i

2019-08-04 07:58发布

问题:

I connect to Oracle database which has NLS_CHARACTERSET (WE8ISO8859P1) , which as far as I know cannot support storing Arabic text.

But Toad for Oracle can read Arabic from this database:

However, I cannot read this using java code.

even I tried to get row of them in bytes using UTL_RAW.CAST_TO_RAW

The result was "218,227,237,225,228,199,32,199,225,218,210,237,210,161,225,222,207,32,199,211,202,229,225,223,202,32,32,56,48,37,32,227,228,32,230,205,207,199,202,32,221,225,237,223,211,32,32,32"

In a test java class, I tried to create new String(new char[]{}) using the above mentioned bytes, with no luck to display Arabic characters.

Any help ? , thank you.

回答1:

This could be caused by quite a few things:

  1. Check the column type in database it should be NVARCHAR not VARCHAR (notice the "N" at the beginning of the word)

  2. Try to put charset=utf8 in the connection string

  3. Convert the byte[] to string using UTF-8 encoding like this

    String arabicText = new String(byteArray, "UTF-8");