0.事由
在使用Spring Data JPA在数据库中插入记录时,发生了如下错误:
SQL Error: 1366, SQLState: 22001
Data truncation: Incorrect string value: '\xE6\xB5\x8B\xE8' for column `database_name`.`table_name`.`column_name` at row 1
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.dao.DataIntegrityViolationException: could not execute statement; SQL [n/a]; nested exception is org.hibernate.exception.DataException: could not execute statement] with root cause
刚开始还以为是数据库连接上出了问题,便尝试使用DataGrip这个数据库管理软件直接插入数据,结果也发生了相同的错误:
Incorrect string value: '\xE6\xB5\x8B\xE8' for column `database_name`.`table_name`.`column_name` at row 1
显然,这是数据库本身的字符集导致的问题了。

当使用Spring Data JPA插入中文记录遇到错误时,发现是数据库字符集问题。通过修改数据库和表的字符集来解决此问题。SQL语句包括:更改数据库默认字符集,以及修改已有表及其字段的字符集设置。
最低0.47元/天 解锁文章

被折叠的 条评论
为什么被折叠?



