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

Unicode characters saved as � using JDBC, but saved correctly using phpMyAdmin

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

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();

Table:

正在插入:

PreparedStatement state = Mysql.conn().prepareStatement("insert into contents(campaignId, site_id, original_article_id, title, content) values(-1, -1, -1, ?, ?)");
state.setString(1, "acbdąčęėįšųū");
state.setString(2, "acbdąčęėįšųū");
state.execute();

结果:

怎么了?

编辑1: 如果我通过 phpMyAdmin 插入,一切都很好:

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

  1. 如何解释字符串文字,以及
  2. 项目是 运行 来自 IDE.
  3. 时 JVM 使用的默认字符集

在这种情况下,快速解决方法是将源文件编码更改为 UTF-8