中文为”???“的解决方法,和Hibernate:Caused by: java.sql.SQLException: Incorrect string value: '\xE8\x8F异常

linux系统下,mysql数据库显示中文为???。

打开 > vi /etc/my.cnf

 [mysqld]
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid 


于是修改此文件内容如下:

[client]
default-character-set = utf8

[mysql]
default-character-set = utf8

[mysqld]
character-set-client-handshake = FALSE
character-set-server = utf8
collation-server = utf8_unicode_ci
max_allowed_packet = 100M
datadir=/var/lib/mysql
socket=/var/lib/mysql/mysql.sock
user=mysql
# Disabling symbolic-links is recommended to prevent assorted security risks
symbolic-links=0

[mysqld_safe]
log-error=/var/log/mysqld.log
pid-file=/var/run/mysqld/mysqld.pid

init_connect='SET NAMES utf8'

此时,如果数据库没有drop掉,就往Hibernate插入数据时,一直报错:

[INFO] Caused by: java.sql.SQLException: Incorrect string value: '\xE8\xAE\xB8\xE8\x8E\x89...' for column 'name' at row 1
[INFO] at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073)
[INFO] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3593)
[INFO] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3525)
[INFO] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1986)
[INFO] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2140)
[INFO] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2626)
[INFO] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2111)
[INFO] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2407)
[INFO] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2325)
[INFO] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2310)
[INFO] at sun.reflect.GeneratedMethodAccessor89.invoke(Unknown Source)
[INFO] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[INFO] at java.lang.reflect.Method.invoke(Method.java:498)
[INFO] at org.hibernate.engine.jdbc.internal.proxy.AbstractStatementProxyHandler.continueInvocation(AbstractStatementProxyHandler.java:122)
[INFO] ... 52 more
Caused by: java.sql.SQLException: Incorrect string value: '\xE8\xAE\xB8\xE8\x8E\x89...' for column 'name' at row 1^CDestroying 1 processes

查找原因是:

导致原因就是数据库和数据表的编码方式导致的。


方法是,drop 掉数据库,重新创建。然后就一切正常了。

相关文章
相关标签/搜索