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 "مرحبا".
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.
New Note:
Database itself don't use UTF16 character set, but
"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."
this note is mentioned here: http://docs.oracle.com/cd/B10501_01/java.920/a96654/advanc.htm