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 + "')");我明白了:
Раунд 338mysql配置:
+--------------------------+----------------------------+
| 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/ |
+--------------------------+----------------------------+表和数据库完全由utf-8编码,我无法从java插入一个西里尔字符串,因为我在表中得到了错误的编码字符串。
我做错什么了?
发布于 2014-01-13 15:14:31
编辑器(IDE)和javac编译器使用相同的编码吗?javac选项是-encoding UTF-8。否则,字符串文本将出现错误的转换。我们可以尝试使用"\u0420\u0430\u0443\u043D\u0434"。如果这样做有效,那么.java的编码就会出现问题。
发布于 2014-01-13 15:01:06
尝试将代码更改为:
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 + "')");https://stackoverflow.com/questions/21094316
复制相似问题