2
Properties properties= new Properties();
properties.setProperty("user", login);
properties.setProperty("password", password);
properties.setProperty("useUnicode","true");
properties.setProperty("characterEncoding","UTF-8");
conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + db, properties);

String message = "Раунд " + nextRoomId;

Data.db.executeUpdate("INSERT INTO chat(message) VALUES('" + message + "')");

I get this:

Раунд 338

mysql config:

+--------------------------+----------------------------+
| Variable_name            | Value                      |
+--------------------------+----------------------------+
| character_set_client     | utf8                       |
| character_set_connection | utf8                       |
| character_set_database   | utf8                       |
| character_set_filesystem | binary                     |
| character_set_results    | utf8                       |
| character_set_server     | utf8                       |
| character_set_system     | utf8                       |
| character_sets_dir       | /usr/share/mysql/charsets/ |
+--------------------------+----------------------------+

the tables and database are fully utf-8 encoded, I can't insert a cyrillic string from java, because I get bad encoded string in my table

what I'am doing wrong?

2 Answers 2

2

Do the editor (IDE) and the javac compiler use the same encoding? The javac option is -encoding UTF-8. Otherwise there is a wrong conversion for string literals. One can try with "\u0420\u0430\u0443\u043D\u0434". If that works, then there is an issue with the encoding of the .java.

Sign up to request clarification or add additional context in comments.

5 Comments

the editor is using utf-8 without BOM, how do I compile the code with utf-8 encoding?, I've tried this: javac -encoding=UTF-8 Main.java, but the -encoding flag is invalid
I've tried javac -encoding utf8 Main.java - that worked great, thank you maan!
Gratulations, UTF-8 I see more and more. Which build system do you use? In maven it is easy: pom.xml, compiler plugin. In ant it is an attribute of the javac task. For the pure taks: javac -encoding=UTF-8 ....
I just use the standart java compiler and a notepad++ :)
If you want simpleness you could give the NetBeans IDE a try; yes eclipse is a bit overloaded. BTW I use NotePad++ regularly and favour knowing what happens. But things like autocompletion, javadoc, JSE sources, integration of maven, vcs are unbeatable.
1

Try to change your code to:

Properties properties= new Properties();
properties.setProperty("user", login);
properties.setProperty("password", password);
properties.setProperty("useUnicode","true");
properties.setProperty("characterEncoding","UTF-8");
conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + db, properties);

String message = "Раунд " + nextRoomId;
String encMessage = new String(message.getBytes(), "UTF-8");

Data.db.executeUpdate("INSERT INTO chat(message) VALUES('" + encMessage + "')");

3 Comments

tryied that , doesn't helped :(
if you try to print message variable what you get it?
This worked for me very nice. I had all the encodings of the project, of the MySQL database to utf-8, however still facing the problem for cyrillic characters. With getBytes() the magic happened. Thank you

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.