简体   繁体   中英

java.sql.ResultSet.getString() returns string with extra characters

I recently encountered a problem with trying to retrieve a CLOB value from Oracle DB with Java . There is a table that stores XML files as CLOB . NLS_CHARACTERSET is set to AL32UTF8 . If I try to retrieve a value with java.sql package and using ResultSet.getClob().getAsciiStream() which then converts to the String with UTF-8 encoding, then I get a valid XML.

But if I use ResultSet.getString() , XML parser fails with parsing exception.

While debugging, the extracted value looks like this . And it contains only half of the file.

Others XML files can be selected with ResultSet.getString() with no problem.
I didn't see any noticeable difference in ASCII representation of corrupted and valid XML.
Problem is fixed when you re-insert the same value to the database.

Can you please explain this behaviour of ResultSet.getString() method?

Information about Oracle

Oracle version is 12.1.0.2.0.  

Information about JDK:

java version "1.7.0_131"  
OpenJDK Runtime Environment (rhel-2.6.9.0.0.1.el7_3-x86_64 u131-b00)  
OpenJDK 64-Bit Server VM (build 24.131-b00, mixed mode)

To me, it looks like that particular clob is not actually encoded in UTF-8 as it claims, but UTF-16. It can occasionally happen that data can be written to a column using a charset other than the NLS_CHARACTERSET . This explains why the problem is fixed when the data is re-inserted using the correct local charset.

I'm guessing that the Clob.getAsciiStream() has extra logic to deal with this sort of thing - maybe for incorrectly encoded overlong (00-padded) UTF-8 , which is indistinguishable from UTF-16 for ASCII code points.

I believe its returning CLOB's stored address for some reason and when you get it with getCLOB it makes it XML.

Check your stored XML you may forget a tag closing or may have some other syntax problem in your stored XML file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM