出错信息:
java.sql.SQLException: Incorrect string value: 'xE4xBDxA0xE5xA5xBD' for column 'username' at row 1
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3558)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3490)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1959)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2109)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2648)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:2077)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2362)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2280)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:2265)
at com.amaker.dao.impl.UserDaoImpl.register(UserDaoImpl.java:46)
at com.amaker.test.UserDaoImplTest.testRegister(UserDaoImplTest.java:34)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at junit.framework.TestCase.runTest(TestCase.java:168)
at junit.framework.TestCase.runBare(TestCase.java:134)
at junit.framework.TestResult$1.protect(TestResult.java:110)
at junit.framework.TestResult.runProtected(TestResult.java:128)
at junit.framework.TestResult.run(TestResult.java:113)
at junit.framework.TestCase.run(TestCase.java:124)
at junit.framework.TestSuite.runTest(TestSuite.java:243)
at junit.framework.TestSuite.run(TestSuite.java:238)
at org.junit.internal.runners.JUnit38ClassRunner.run(JUnit38ClassRunner.java:83)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
分析原因:编码不一致导致
解决方案:
(如果你用的是java编程访问数据库,前提是java编码也要和数据库编码一致)
1.检查数据库编码与表的编码是否一致,发现数据库编码utf8,校对规则为utf8_unicode_ci,而表的编码为latin1,校对规则为Latin1_general_ci,
修改表的编码和校对规则,使其与数据库一致,这个可以在更改表里面的高级属性部分修改,重启数据库后,再次插入中文数据,还是出现问号;
2、导出表结构,从sql语句中发现字段编码依然是latin1,
修改字段编码,执行以下sql语句如下:
ALTER TABLE `user` CHANGE `username` `username` VARCHAR(20) COLLATE utf8_unicode_ci DEFAULT NULL;
ALTER TABLE `user` CHANGE `password` `password` VARCHAR(20) COLLATE utf8_unicode_ci DEFAULT NULL;
最后重启数据库,中文数据插入数据库,问题解决。