Strange behavior when converting a string to a UTF-8 character

I have a data type that is stored as blob (XML) in Oracle DB. I am extracting this column and converting it to byte [] and then to string. I perform some string operations and convert them to UTF-8 format and insert them back into the database. Some special characters are inserted as unwanted characters. I don’t know what am I doing wrong? Any idea / help would be greatly appreciated.

Here is the source code.

     // DB COnnection
     // Get the resultset

     Blob data = (Blob) rs.getBlob(3);
 byte[] bdata = data.getBytes(1,(int)data.length());  

 // Converting to String and doing operation
 s = new String(bdata);   
 // String operation


 // Before inserting into DB, converting to UTF-8 format.
 byte[] dataAsByteArray = s.getBytes("UTF8");  
 updateStmt.setBinaryStream(1,
          new  ByteArrayInputStream(dataAsByteArray),dataAsByteArray.length);

 commit();
+3
source share
1 answer

BLOB UTF-8? XML, XML API . ( ) . . DocumentBuilder BLOB InputStream, byte-to-char .

, String, byte-to- char.

 // Converting to String and doing operation
 s = new String(bdata);

, JVM (doc ). , . BLOB byte-to- char String.

, , UTF8 UTF-8 . , , , , , .

+2

Source: https://habr.com/ru/post/1730130/


All Articles