I try to deploy a Spring project on the openshift service. I'm trying to write to the database in utf-8 encoding. However, Russian letters fall into the database table in the following form:

ÑÑмÑÑмÑÑм 

That is, in ISO 8859-1 (latin-1).

in the URL by which Hibernate is connected to the database, I specify the UTF-8 encoding:

 jdbc:mysql://${OPENSHIFT_MYSQL_DB_HOST}:${OPENSHIFT_MYSQL_DB_PORT}/jbossews?useUnicode=yes&characterEncoding=UTF-8 

I specify utf-8 in JSP

 <%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %> <form action="addMessage" method="get" accept-charset="utf-8"> <textarea maxlength="600" name="message"></textarea><br/> <input type="submit" id="button" value="отправить сообщение"/> </form> 

I create base with such command:

 create database jbossews default character set = "UTF8" default collate = "utf8_general_ci" 

Such a filter in web.xml does not bring any results:

 filter> <filter-name>encoding-filter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> <init-param> <param-name>forceEncoding</param-name> <param-value>true</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding-filter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> 

It is not possible to change some settings in the my.conf file. Mysql, because changes to this file are reset after restarting the database. Here is the database itself:

  mysql> SHOW VARIABLES LIKE'character%'; 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 /opt/rh/mysql55/root/usr/share/mysql/charsets/ SHOW VARIABLES LIKE 'collation%'; Variable_name Value collation_connection utf8_general_ci collation_database latin1_swedish_ci collation_server latin1_swedish_ci 

Help solve the problem with writing in the wrong encoding.

  • Please specify the question . - aleksandr barakin
  • What encoding of the HTML page is determined by the browser? - etki
  • '<% @ page contentType = "text / html; charset = UTF-8" language = "java"%>' utf-8 is indicated in the page header, in the database itself also krakozyaby. - fantastic
  • one
    @fantastic Try replacing utf8_unicode_ci with utf8_general_ci. - Vladimir Glinskikh
  • Does not help. slammed the base and recreated again: 'CREATE DATABASE jbossews CHARACTER SET utf8 COLLATE utf8_general_ci;' in hibernet settings: '# db.url =. .. jbossews? useUnicode = yes & characterEncoding = UTF-8 'in the title of the page: contentType = "text / html; charset = UTF-8" Everywhere, like, set utf-8 and still a coding error. It is in the table that the records are stored in the wrong encoding. If you make an entry directly in phpMyAdmin in Russian letters in the table, it is correctly displayed in the table and on the page. And when the application makes an entry in the database, it turns out kozyabry. - fantastic

2 answers 2

If, as you said, new entries in Cyrillic are displayed normally when encoding is UTF-8 , then everything is fine with the base. The old records, in which, as I understand it, is your problem, are displayed incorrectly, because they are in an encoding that differs from UTF-8.

This means that all existing entries need to be rewritten in UTF-8.

  • New entries, like old ones, get from the application to the database in ISO-8859-1 encoding instead of utf8. - fantastic
  • Very strange behavior. Try to put in the application (what?) Encoding utf8. - Daniel Shatz
  • There is one bad thing about how I do it all, can it influence the result? The project works fine on a local database with the following URL: db.url = jdbc: mysql: // localhost: 3306 / test? useUnicode = yes & characterEncoding = UTF-8. But I want to host the application on a remote hosting, where the DB URL is: mysql: // $ OPENSHIFT_MYSQL_DB_HOST: $ OPENSHIFT_MYSQL_DB_PORT / jbossews. I register this URL, I start to build the project, a .war file is created, then a local error raises errors that could not connect to the database. And ... I am sending this .war file to the hosting. Where everything seems to work, but there are no Russian letters. - fantastic

He himself struggled with this for some time, everything turned out to be very simple:

 jdbc:mysql://localhost:3306/quotesdb?useUnicode=true&amp;characterEncoding=utf8 

Those. not characterEncoding=UTF-8 , but characterEncoding=utf8

And that's all. Well, of course, you need to check the database itself and how it was created.