俄语字符存储为????在grails应用程序中的mysql中

时间:2016-01-15 12:25:32

标签: mysql grails jdbc character-encoding gorm

我正在尝试使用grails应用程序将俄语字符保存到mysql中,但是,在DB中它将存储为????

我的数据源映射:

 dataSource {
    pooled = true
    driverClassName = "com.mysql.jdbc.Driver"
    dialect = "org.hibernate.dialect.MySQL5InnoDBDialect"
    username = "sa"
    password = ""
}
....
url = "jdbc:mysql://localhost/mydb?useUnicode=true&zeroDateTimeBehavior=convertToNull&characterEncoding=UTF-8"

我的域类

class Lang {

    String langText

    static constraints = {
        langText nullable: true, blank: true
    }

    static mapping = {
        langText type: 'text'
    }
}

lang表的编码为utf8

mysql> SELECT CCSA.character_set_name FROM information_schema.`TABLES` T,
    ->        information_schema.`COLLATION_CHARACTER_SET_APPLICABILITY` CCSA
    -> WHERE CCSA.collation_name = T.table_collation
    ->   AND T.table_name = "lang";
+--------------------+
| character_set_name |
+--------------------+
| utf8               |
+--------------------+
1 row in set (0.01 sec)

lang_text的编码为utf8

mysql>   SELECT character_set_name FROM information_schema.`COLUMNS` 
    -> WHERE  table_name = "lang"
    ->   AND column_name = "lang_text";
+--------------------+
| character_set_name |
+--------------------+
| utf8               |
+--------------------+
1 row in set (0.01 sec)

这就是我将信息保存到数据库的方式:

def lang() {
    Lang l = new Lang()
    l.langText = "Здравствуйте, меня зовут Энтони";
    l.save(flush: true)
}

但是当我查看数据库时,信息存储为?????

mysql> select lang_text from lang where id = (select max(id) from lang);
+---------------------------------+
| lang_text                       |
+---------------------------------+
| ????????????, ???? ????? ?????? |
+---------------------------------+
1 row in set (0.00 sec)

这似乎是一个GORM / Hibernate问题,因为当我使用mysql将字符串输入数据库时​​,它添加正常:

mysql> insert into lang (lang_text) values ("Здравствуйте, меня зовут Энтони");
Query OK, 1 row affected, 1 warning (0.10 sec)

mysql> select lang_text from lang where id = (select max(id) from lang);
+------------------------------------------------------------+
| lang_text                                                  |
+------------------------------------------------------------+
| Здравствуйте, меня зовут Энтони                            |
+------------------------------------------------------------+
1 row in set (0.05 sec)

1 个答案:

答案 0 :(得分:1)

我能解决这个问题。我的jdbc网址中有&。它应该是&

希望这可以节省一些人花在这个> _<

上的时间