[ACCEPTED]-Java PreparedStatement UTF-8 character problem-character-encoding

Accepted answer
Score: 41

The number of ways this can get screwed 5 up is actually quite impressive. If you're 4 using MySQL, try adding a characterEncoding=UTF-8 parameter to 3 the end of your JDBC connection URL:


You 2 should also check that the table / column 1 character set is UTF-8.

Score: 7

Whenever a database changes a character 24 to ?, then it simply means that the codepoint 23 of the character in question is completely 22 out of the range for the character encoding 21 as the table is configured to use.

As to 20 the cause of the problem: the ç lies within 19 ISO-8859-1 range and has exactly the same codepoint 18 as in UTF-8 (U+00E7). However, the UTF-8 codepoint 17 of ş lies completely outside the range of 16 ISO-8859-1 (U+015F while ISO-8859-1 only goes 15 up to U+00FF). The DB won't persist the 14 character and replace it by ?.

So, I suspect 13 that your DB table is still configured to 12 use ISO-8859-1 (or in one of other compatible 11 ISO-8859 encodings where ç has the same codepoint 10 as in UTF-8).

The Java/JDBC API is doing 9 its job perfectly fine with regard to character 8 encoding (Java uses Unicode all the way) and 7 the JDBC DB connection encoding is also 6 configured correctly. If Java/JDBC would 5 have incorrectly used ISO-8859-1, then the 4 persisted result would have been Åakça (the ş exist 3 of bytes 0xC5 and 0x9F which represents Å and a in 2 ISO-8859-1 and the ç exist of bytes 0xC3 and 1 0xA7 which represents à and § in ISO-8859-1).

Score: 3

setString methods changes 'şakça' to '?akça'

How 7 do you know that setString changes this? Or 6 do you see the content in the database and 5 decide this?

It could be that the database 4 is not configured for UTF-8, or simply that 3 the tool you use to see the contects of 2 the database (SQL*PLUS for Oracle...) is 1 not capable of diaplaying UTF-8.

Score: 0

you can use query as below to set unicode 1 strings in prepared statement. PreparedStatement st= conn.prepareStatement("select * from users where username=unistr(?)");// unistr method is for oracle st.setString(1, userName);

More Related questions