com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: ‘4‘ for

本文解析了一起因数据类型不匹配导致的MySQL插入错误案例,详细介绍了错误发生的原因及解决方案,强调了正确设置数据库字段类型的重要性。

错误:

com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Incorrect datetime value: '4' for column 'repair_status' at row 1
    at com.mysql.cj.jdbc.exceptions.SQLExceptionsMapping.translateException(SQLExceptionsMapping.java:104)
    at com.mysql.cj.jdbc.ClientPreparedStatement.executeInternal(ClientPreparedStatement.java:953)
    at com.mysql.cj.jdbc.ClientPreparedStatement.execute(ClientPreparedStatement.java:370)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3461)
    at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
    at com.alibaba.druid.wall.WallFilter.preparedStatement_execute(WallFilter.java:626)
    at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:3459)
    at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:167)
    at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:497)
    at sun.reflect.GeneratedMethodAccessor285.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
    at com.sun.proxy.$Proxy369.execute(Unknown Source)
    at org.apache.ibatis.executor.statement.PreparedStatementHandler.update(PreparedStatementHandler.java:47)
    at org.apache.ibatis.executor.statement.RoutingStatementHandler.update(RoutingStatementHandler.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63)
    at com.sun.proxy.$Proxy367.update(Unknown Source)
    at com.baomidou.mybatisplus.core.executor.MybatisSimpleExecutor.doUpdate(MybatisSimpleExecutor.java:56)
    at org.apache.ibatis.executor.BaseExecutor.update(BaseExecutor.java:117)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.update(DefaultSqlSession.java:197)
    at org.apache.ibatis.session.defaults.DefaultSqlSession.insert(DefaultSqlSession.java:184)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:426)
    at com.sun.proxy.$Proxy177.insert(Unknown Source)
    at org.mybatis.spring.SqlSessionTemplate.insert(SqlSessionTemplate.java:271)
    at com.baomidou.mybatisplus.core.override.MybatisMapperMethod.execute(MybatisMapperMethod.java:60)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy$PlainMethodInvoker.invoke(MybatisMapperProxy.java:148)
    at com.baomidou.mybatisplus.core.override.MybatisMapperProxy.invoke(MybatisMapperProxy.java:89)
    at com.sun.proxy.$Proxy272.insert(Unknown Source)
    at com.baomidou.mybatisplus.extension.service.IService.save(IService.java:61)
    at com.baomidou.mybatisplus.extension.service.IService$$FastClassBySpringCGLIB$$f8525d18.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
    at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:687)
    at com.iwater.workbench.data.feign.service.impl.DeviceExceptionRepairServiceImpl$$EnhancerBySpringCGLIB$$778e5bdd.save(<generated>)
    at com.iwater.workbench.data.service.impl.AreaServiceImpl.repairManually(AreaServiceImpl.java:1086)
    at com.iwater.workbench.data.service.impl.AreaServiceImpl$$FastClassBySpringCGLIB$$bbfece95.invoke(<generated>)
    at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)

原因:

入参repair_status类型错误填的Interger,而表中要求datetime

deviceExceptionRepair = {DeviceExceptionRepair@19135} 
 isOnline = null
 lastOnlineTime = null
 updateTime = null
 exceptionId = null
 deviceId = null
 deviceCode = "865118046744269"
 deviceType = null
 deviceCategory = null
 repairStatus = {Integer@19321} 4
 repairResult = "修复成功"
 repairType = {Integer@19373} 2
 createTime = {DateTime@19400} "2022-11-23 14:46:42"
 companyId = {Long@18460} 12
 id = {Long@19167} 1595307828788002907
 SuperEntity.createTime = null
 SuperEntity.updateTime = null
 log = {StdOutImpl@19161} 

解决:

修改表中repair_status类型为tinyint

### 解决 MySQL 数据截断错误 `Data truncation: Data too long for column 'code' at row 1` 当出现 `com.mysql.cj.jdbc.exceptions.MysqlDataTruncation: Data truncation: Data too long for column 'code' at row 1` 错误时,表示插入或更新数据库记录时,字段 `code` 的数据长度超过了数据库表中该字段定义的最大长度限制。以下是具体的解决方案: #### 1. 检查字段定义并调整字段长度 首先,需要检查数据库表中 `code` 字段的定义。可以通过以下 SQL 语句查看字段的结构: ```sql DESCRIBE your_table_name; ``` 假设 `code` 字段当前定义为 `VARCHAR(50)`,但实际插入的数据长度超过 50 个字符,则需要调整字段长度。例如,将其扩展为 `VARCHAR(255)`: ```sql ALTER TABLE your_table_name MODIFY code VARCHAR(255); ``` 如果字段使用的是 `CHAR` 类型,也可以根据需求改为 `VARCHAR`,以节省存储空间。 #### 2. 验证插入或更新的数据长度 在代码层面,建议在执行插入或更新操作之前,对 `code` 字段的数据长度进行校验,确保其不会超过数据库字段定义的最大长度。例如,在 Java 中可以使用如下逻辑: ```java if (code != null && code.length() > 255) { throw new IllegalArgumentException("code 字段长度不能超过 255"); } ``` #### 3. 启用宽松模式避免严格模式下的数据截断异常 MySQL 的 SQL 模式(SQL Mode)会影响数据插入时的校验行为。如果启用了 `STRICT_TRANS_TABLES` 或 `STRICT_ALL_TABLES` 模式,MySQL 会在数据长度超过限制时直接抛出错误。可以通过修改 MySQL 的配置文件(如 `my.cnf` 或 `my.ini`)来禁用严格模式: ```ini [mysqld] sql_mode=NO_ENGINE_SUBSTITUTION,NO_ZERO_DATE,NO_ZERO_IN_DATE ``` 修改后重启 MySQL 服务以生效配置。此方法适用于开发或测试环境,但在生产环境中应谨慎使用,以避免数据不一致问题。 #### 4. 使用日志和调试工具定位数据来源 如果 `code` 字段的数据来源于用户输入或外部系统,建议在应用层记录详细的日志,包括插入或更新的数据内容,以便快速定位导致问题的具体数据。例如,在 Java 应用中可以使用日志框架记录相关信息: ```java logger.debug("Inserting code value: {}", code); ``` #### 5. 使用数据库迁移工具管理字段变更 在生产环境中修改数据库字段结构时,建议使用数据库迁移工具(如 Flyway 或 Liquibase)来管理变更。这可以确保字段修改操作在多个环境中保持一致,并且具备回滚能力。 ---
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值