Java PreparedStatement setString changes characters

荒凉一梦 提交于 2019-12-01 23:55:11

问题


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 actually don't know where to search for things that should repair it, so I don't know if code is required.. Anyway, I'll put some here:

PreparedStatement stm = null;
String sql = "";

    try{
      sql = "INSERT INTO methods (name, description) VALUES (?, ?)";
      stm = connection.prepareStatement(sql);
      stm.setString(1, method.getName());
      stm.setString(2, method.getDescription());
      //...
    }catch(Exception e){}

while debugging 'name' field was correct in method object, but after adding it into stm variable, it changed it's characters to '?'.

I have found one topic about the similar sitoatuin on SO, but there wasn't any answer that could help me since I exactely know that there is something not right in adding string to statement, not in database. But I don't know what..

Any sugestions?

PS. I'm using netbeans 6.7.1 version

EDIT: I was debugging with standard netbeans debugger, and was checking state of variables before adding strings to 'stm' variable. I was even changing getName() method to static string with special characters. So for sure everything is ok with Method class.

EDIT2: I've made one more test. Checked stm variable and one of it's properties is "charEncoding" which is set to "cp1252". So the main question is.. how to change that?


回答1:


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.




回答2:


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)




回答3:


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



来源:https://stackoverflow.com/questions/4724299/java-preparedstatement-setstring-changes-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!