开发者

Java PreparedStatement setString changes characters

开发者 https://www.devze.com 2023-02-05 02:29 出处:网络
As in title: to be sure, I was debugging my application, and so in line, where I put strings into PreparedSta开发者_运维技巧tement variable, special characters are changing to \"?\". I actually don\'t

As in title: to be sure, I was debugging my application, and so in line, where I put strings into PreparedSta开发者_运维技巧tement 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?


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)


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.


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

精彩评论

暂无评论...
验证码 换一张
取 消