今天在做开关记录的时候,数据库表中description字段用于记录当前版本的描述(需要存储中文),在测试程序的时候发现如果用户输入中文,那么后台会报错。错误信息如下:
java.sql.SQLException: Incorrect string value: '\xE8\xAF\xA6\xE6\x83\x85...' for column 'description' at row 1 at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1073) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3609) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3541) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2002) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2163) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2624) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2127) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2427) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2345) ~[mysql-connector-java-5.1.18.jar:na] at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2330) ~[mysql-connector-java-5.1.18.jar:na] at sun.reflect.GeneratedMethodAccessor263.invoke(Unknown Source) ~[na:na] at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.7.0_79] at java.lang.reflect.Method.invoke(Method.java:606) ~[na:1.7.0_79] at org.logicalcobwebs.proxool.ProxyStatement.invoke(ProxyStatement.java:100) ~[com.cloudhopper.proxool-proxool-0.9.1.jar:na]
通过查看表信息发现description字段默认的字符集为:latin1 [QAQ]
好吧,定位到问题,那么接下来就是修改字符集了。
尝试将字符集修改成utf8后问题解决。
ALTER TABLE `masala_switch` MODIFY ) CHARACTER SET utf8 NULL DEFAULT NULL COMMENT '版本描述';