Unicode字符使用JDBC保存为,,但使用phpMyAdmin正确保存

时间:2016-11-27 19:27:29

标签: sql jdbc unicode utf-8 utf8mb4

以下是我创建连接的方法:

if userCookie != nil, let cookieStorage = Alamofire.SessionManager.default.session.configuration.httpCookieStorage {
        cookieStorage.setCookies([userCookie!], for: url, mainDocumentURL: nil)
    }

表:

enter image description here

插入:

Class.forName("com.mysql.jdbc.Driver");
Properties properties = new Properties();
properties.setProperty("user", "root");
properties.setProperty("password", "PASSWORD");
properties.setProperty("useSSL", "false");
properties.setProperty("autoReconnect", "true");
properties.setProperty("useUnicode", "true");
properties.setProperty("characterEncoding", "UTF-8");
Connection connection =  DriverManager.getConnection("jdbc:mysql://localhost/coolpoop", properties);

PreparedStatement statement = connection.prepareStatement("SET NAMES 'utf8'");
statement.execute();
statement = connection.prepareStatement("SET CHARACTER SET utf8");
statement.execute();

结果:

enter image description here

有什么问题?

EDIT1: 如果我通过phpMyAdmin插入,所有好处: enter image description here

1 个答案:

答案 0 :(得分:1)

在某些情况下,Java开发工具(例如Eclipse)对Java源文件使用Cp1252(a.k.a。windows-1252)的默认字符编码。使用Unicode字符时,这可能会导致奇怪的编码问题,因为源文件编码会影响

  1. 如何解释字符串文字,
  2. 从IDE中运行项目时JVM使用的默认字符集。
  3. 在这种情况下,快速解决方法是将源文件编码更改为UTF-8