Java,MySQL:我保存了“ČeskáTřebová”,但保存了“?eskáT?ebová”(来自终端一切都很好)

当我从终端插入所有好的:插入城镇价值观(113,’?eskT?ebov’,22,“测试”,真实);但是当我通过JDBC手动保存时:java.sql.Connection conn = null;Statement stmt = null;Class.forName(com.mysql.jdbc.Driver);...

当我从终端插入所有好的:
插入城镇价值观(113,’?eskáT?ebová’,22,“测试”,真实);

但是当我通过JDBC手动保存时:

java.sql.Connection conn = null;
Statement stmt = null;
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/a2b", "root", "root");
stmt = conn.createStatement();
stmt.executeUpdate("INSERT INTO towns VALUES (200, '?eská T?ebová', 22, 'test', true);");

…或者来自Hibernate的持久化对象:

String townName = new String("?eská T?ebová".getBytes(), "UTF-8");
townDao.persist(new Town(townName, CountryCode.AQ, "test", true));

……或者使用PreparedStatement:

PreparedStatement addTown = null;
String addTownPrepared = "INSERT INTO towns VALUES (1100, ?, 22, 'test', true)";
addTown = conn.prepareStatement(addTownPrepared);
addTown.setString(1, townName);
addTown.executeUpdate();

……我在MySQL 5.5上看到了?eskáT?ebová

我在数据库中有许多名为Göppingen,Würzburg,Kolín的城市 – 问题不在于所有非拉丁符号.

========================================

我在Lubuntu 14.04下.

show variables like '%char%';

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

在pom.xml中我有

<integration-test.jdbc.url><![CDATA[jdbc:mysql://localhost:3306/a2b?useUnicode=yes&characterEncoding=UTF-8]]></integration-test.jdbc.url>

我的桌子:

CREATE TABLE towns (
  id                  BIGINT            AUTO_INCREMENT      ,
  name                VARCHAR (256)     NOT NULL            ,
  country             SMALLINT          NOT NULL            ,
  source              VARCHAR (256)                         ,
  is_active           BIT               DEFAULT 1           ,
  PRIMARY KEY (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8;

Workbench的屏幕截图:

解决方法:

注意URL到数据库!您确定要编辑您认为的URL吗?

在我的pom.xml中,我有:

< integration-test.jdbc.url>,< test.jdbc.url>和< jdbc.url>.

我需要更改< jdbc.url>从jdbc:mysql:// localhost:3306 / a2b到<![CDATA [jdbc:mysql:// localhost:3306 / a2b?useUnicode = yes& characterEncoding = UTF-8]]>

本文标题为:Java,MySQL:我保存了“ČeskáTřebová”,但保存了“?eskáT?ebová”(来自终端一切都很好)

基础教程推荐