21

I'm having trouble sending or displaying text with special characters from my webservice to my database. On my eclipse I have set the character encoding to UTF-8 but it still doesn't let me display the characters. For example a simple print like the code below

String test ="привет"; 
System.out.println(test);

OR

String test ="привет";
String query = "insert into communication (`test`) VALUES ('"+ test +"');
PreparedStatement preparedStmt1 = con.prepareStatement(query);
preparedStmt1.executeUpdate();

The result on the console and if I send this to my database is ??????. How do I get this to display correctly on the console and hopefully in the database

Amanni
  • 1,924
  • 6
  • 31
  • 51
  • Is this the eclipse console, or sh/cmd? what character set is your console set to? – atk Jun 07 '12 at 19:11
  • 1
    eclipse, if by console character set you mean the settings in Windows > Preferences > WorkSpace it is set to UTF-8. Is there another way to set the console separately? – Amanni Jun 08 '12 at 09:59

4 Answers4

13

If you are using Eclipse, then

  1. right click on you project.
  2. Go to Properties
  3. Select UTF-8 in "Text File Encoding "

enter image description here

Burhan ARAS
  • 2,517
  • 25
  • 19
10

See if this works.

    PrintStream out = new PrintStream(System.out, true, "UTF-8");
    out.println(test);

For storing in DB, use following to explicitly encode the string in UTF-8

String newString = new String(test.getBytes(), "UTF8");
Santosh
  • 17,667
  • 4
  • 54
  • 79
3

Yeah, its the XXI. century and we're still struggling with things like character encoding...

My first guess is that either:

  1. your source file encoding might be wrong (do you use build tools like Maven? It might be needed to set the source-encoding there too),
  2. your console encoding might be wrong (are you under Windows? The default command line console is not UTF by default, its local-dependant, but with a small play in the registry you can set its encoding)
  3. your DB encoding might be incorrect (what is the Table encoding, can you check that?)
rlegendi
  • 10,466
  • 3
  • 38
  • 50
  • I've set the table encoding to utf8 still the same result. As far as the encoding goes I can display special characters on when I program in android. – Amanni Jun 07 '12 at 14:58
0

may be you need to decode your character string to ISO-8859 first and then encode it to UTF-8

wyk
  • 1