message = new String(("round " + id).getBytes("UTF-8"));
conn = DriverManager.getConnection("jdbc:mysql://" + host + "/" + db + "?useUnicode=true&characterEncoding=UTF-8&"
+ "user=" + login + "&password=" + password);
When I make an insert into the database which encoding is UTF-8 CI
, get something like this �������������������� 179
, the java file encoding is utf-8, what I'am doing wrong?
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+
Generally, MySQL comes with a list of predefined system variables. If you want to list them, you can open the MySQL prompt and type:
As shown, MySQL's default encoding is
latin1
. In order to change it, you need to edit a bit your my.cnf file and add the following lines:When you configure the my.cnf file and restart the MySQL server, you'll note the difference.
Edit: You can set the encoding for the JDBC's
DriverManager
like this:Besides set connection string in java jdbc,An alternative way to change mysql charset:
1) specify utf8 when creating database and table
2) or alter it after creating(I don't encourage this)
see mysql for more help.