Java PreparedStatement setString changes characters

As in the header: I, of course, debugged my application, and therefore in the line where I put the lines in the PreparedStatement variable, the special characters change to "?". Actually, I don’t know where to look for things that should fix it, so I don’t know if the code is needed .. In any case, I will add some of them:

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 the name field was correct in the method object, but after adding it to the stm variable, it changed the characters to "?".

I found one topic about a similar sitoatuin on SO, but there was no answer that could help me, since I know for sure that there is something wrong when you add a line to the statement and not to the database. But I do not know what ..

Any sugestions?

PS. I am using netbeans version 6.7.1

EDIT: I debugged the standard netbeans debugger and checked the state of the variables before adding lines to the stm variable. I even changed the getName () method to a static string with special characters. So, everything is fine with the Method class.

EDIT2: I did another test. The stm variable to be checked and one of its properties is "charEncoding", which is set to "cp1252". So, the main question: how to change this?

+3
source share
3 answers

Sounds like a character encoding problem to me. Perhaps the driver will transcode your lines into the appropriate encoding for the field / table / schema / database, and does not allow the server to do this? If you are trying to save a character that does not have a field / table / schema / database encoding, this explains "?" characters.

+1
source

this usually happens in using different encodings in different places. it looks like you are getting your input as UTF-8, converting it to another chatset (maybe your database is set up for something else), which breaks the special character.

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

* look at this or my answer on another thread (as for the problem in php, but in java it is almost the same)

+3
source

Do you use Oracle ? I had similar situations if the environment variables related to character sets were not correctly defined.

By default, the Oracle connection is ASCII (7-bit characters, AZ, az, numbers, punctuation, ...). If you use any character outside this (for example, European accents, Chinese characters, ..), then you need to use something other than ASCII. UTF-8 is better. If you do not, your characters will be replaced by "?".

You need your system administrator to install this for you. Alternatively look here:

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

+1
source

Source: https://habr.com/ru/post/1400147/


All Articles