3

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?

Seraphis
  • 1,026
  • 2
  • 14
  • 30
  • How were you checking this? What tool did you use to verify the data in the database? – skaffman Jan 18 '11 at 13:01
  • Are you saying that the return value of `method.getName()` changed after it has been called? – Joachim Sauer Jan 18 '11 at 13:01
  • *Never* do `catch(Exception e){}` - if an exception happens, you'll not know that it did and what went wrong. At the very least print the stack trace: `catch (Exception e) { e.printStackTrace(); }` – Jesper Jan 18 '11 at 13:20
  • Having the same issue but not fixed yet.. i am using Mysql Workbench 5.2 and Java 7. Kindly help me to fix this – dbyuvaraj May 16 '13 at 09:31

4 Answers4

3

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)

Community
  • 1
  • 1
oezi
  • 51,017
  • 10
  • 98
  • 115
  • But the strings aren't going through DB. They are taken from one variable into another one in other file (java class).. JDBC driver changes charset in PreparedStatement variable? – Seraphis Jan 18 '11 at 13:21
  • See my answer below. It's possible that the JDBC driver is transcoding the String from Java's UTF-8 into the appropriate encoding for the database prior to sending it to the database. – dty Jan 18 '11 at 15:44
1

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

Adrian Smith
  • 17,236
  • 11
  • 71
  • 93
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.

dty
  • 18,795
  • 6
  • 56
  • 82
0

I do this :

PreparedStatement ps = null;
List varBind       = new ArrayList();
String strSQL ="INSERT INTO table..... ";
varBind.add(claveEPO); //1
varBind.add(cg_num_distribuidor); //2
varBind.add(numero_docto); //3

// it's importan to do this
byte ptextRazon[] = cgRazonSocialEpo.getBytes("ISO-8859-1");
String razonSocial = new String(ptextRazon, "UTF-8");
varBind.add(razonSocial);//26

ps = con.queryPrecompilado(strSQL.toString(), varBind);
ps.executeUpdate();
ps.close();
Procrastinator
  • 2,526
  • 30
  • 27
  • 36