1.問題描述:
在mysql插入數據的時候報錯:Cause: com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'xxx_name' at row 1
嚴重: Servlet.service() for servlet default threw exceptio com.mysql.jdbc.MysqlDataTruncation: Data truncation: Data too long for column 'xxx_name' at row 1 at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:2868) at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:1573) at com.mysql.jdbc.ServerPreparedStatement.serverExecute(ServerPreparedStatement.java:1160) at com.mysql.jdbc.ServerPreparedStatement.executeInternal(ServerPreparedStatement.java:685) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1400) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1314) at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1299) at com.mchange.v2.c3p0.impl.NewProxyPreparedStatement.executeUpdate(NewProxyPreparedStatement.java:105) at org.springframework.jdbc.core.JdbcTemplate$2.doInPreparedStatement(JdbcTemplate.java:745) at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:538) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:739) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:797) at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:805) at com.bester.blog.dao.impl.UserDAOImpl.add(UserDAOImpl.java:25) at com.bester.blog.service.impl.UserServiceImpl.add(UserServiceImpl.java:12) at com.bester.blog.web.action.UserAction.add(UserAction.java:30) 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)
2.問題分析:
從字面意思看,是字段長度不夠。即使設置為了text還是不夠,因為text類型是可變長度的字符串,最多65535個字符,所有,最好是把字段類型設置為longtext,最多存放4294967295個字符
步驟:進入mysql,use你要改變的數據庫,執行語句:alter table testTable modify column xxx_name longtext;
重啟mysql,linux下語句:service mysqld restart
但是,我的xxx_name字段設置的是varchar(50),而輸入的沒幾個字;
原因在於:建表的時候,表的編碼和字段編碼都是默認的latin1,設置為utf8后,問題解決。修改utf-8編碼