Java,MySQL:我保存了“ČeskáTřebová”,但保存了“?eskáT?ebová”(来自terminal all good)
当我从终端插入“所有良好”时: 在城镇中插入值113,‘ČeskáTřebová’,22,测试,正确 但当我通过JDBC手动保存时:Java,MySQL:我保存了“ČeskáTřebová”,但保存了“?eskáT?ebová”(来自terminal all good),java,mysql,hibernate,Java,Mysql,Hibernate,当我从终端插入“所有良好”时: 在城镇中插入值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");
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 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á
我在数据库中找到了许多城市的名字,如戈皮根、维尔茨堡、科尔林,这些城市并非所有非拉丁符号都有问题
========================================
我在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中,我有
工作台屏幕截图:
小心数据库的URL!你确定你编辑了你认为正确的URL吗 在我的pom.xml中,我有: ,及
我需要从jdbc更改为:mysql://localhost:3306/a2b 为了您还需要在表列/表定义中设置UTF-8编码。这可能有助于您在utf8中@Yagnesh My tables和columns-插入或选择带有非拉丁字符的终端时没有问题,如Č-我认为有问题JDBC..DriverManager.getConnectionjdbc:mysql://localhost:3306/a2b?useUnicode=true&characterEncoding=UTF-根,根;您是否验证了您的Java源文件是使用UTF-8编码保存的?您不知道在新的String..getBytes模式中执行什么操作。这是一个典型的错误模式,造成角色丢失和性能下降。
<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;