Java PreparedStatement setString changes characters

前端 未结 3 1655
北海茫月
北海茫月 2021-01-20 05:00

As in title: to be sure, I was debugging my application, and so in line, where I put strings into PreparedStatement variable, special characters are changing to \"?\". I act

相关标签:
3条回答
  • 2021-01-20 05:32

    Sounds like a character encoding issue to me. Perhaps the driver is transcoding your strings into the appropriate encoding for the field/table/schema/database rather than letting the server do it? If you are trying to store a character which has no representation in the encoding of the field/table/schema/database, that would explain the '?' characters.

    0 讨论(0)
  • 2021-01-20 05:32

    this normally happens by using different charsets in different locations. sound like you're getting your input as UTF-8, converting it to another chatset (maybe your database is set to something else) which breaks the special character.

    to fix this: use the same charset everywhere*. (i would recommend using UTF-8)

    *take a look at this or my answer to another thread (that's about a problem in php, but in java it's almost the same)

    0 讨论(0)
  • 2021-01-20 05:42

    Are you using Oracle? I have had similar situations, if the environment variables regarding character sets weren't defined correctly.

    By default, an Oracle connection is ASCII (7-bit characters, A-Z, a-z, numbers, punctuation, ...). If you use any character outside of that (e.g. European accents, Chinese characters, ..) then you need to use something other than ASCII. UTF-8 is best. If you don't, your characters will get replaced by "?".

    You'd need to get your sysadmin to set this up for you. Alternatively take a look here:

    http://arjudba.blogspot.com/2009/02/what-is-nlslang-environmental-variable.html

    0 讨论(0)
提交回复
热议问题