Java, MySQL: I save "Česká Třebová", but retained "? Eská T? Ebová" (everything is fine from the terminal)

When I paste from the terminal, everything is fine: INSERT INTO towns VALUES (113, 'Česká Třebová', 22, "test", true);

But when I manually save via 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);");

... or save an object from Hibernate:

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

... or using PreparedStatement:

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

... I see ?eská T?ebováin MySQL 5.5

I have in the database are many cities named as a Göppingen, Würzburg, Kolín- the problem with all non-Latin characters.

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

I'm on 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/ |
+--------------------------+----------------------------+

In pom.xmli have

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

My table:

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;

Screenshot from Workbench: enter image description here

+4
source share
1 answer

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]]>

+1

Source: https://habr.com/ru/post/1544896/


All Articles