一、问题场景
只是一个普通方法,突然报了以下错误:
Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.jdbc.UncategorizedSQLException:
### Error querying database. Cause: java.sql.SQLException: Incorrect DATETIME value: '+1000000000-01-01 00:00:00'
### The error may exist in file [D:\IdeaProjects\sky-take-out\sky-server\target\classes\mapper\admin\ReportMapper.xml]
### The error may involve defaultParameterMap
### The error occurred while setting parameters
### SQL: select count(id) from orders WHERE order_time >= ?
### Cause: java.sql.SQLException: Incorrect DATETIME value: '+1000000000-01-01 00:00:00'
; uncategorized SQLException; SQL state [HY000]; error code [1525]; Incorrect DATETIME value: '+1000000000-01-01 00:00:00'; nested exception is java.sql.SQLException: Incorrect DATETIME value: '+1000000000-01-01 00:00:00'] with root cause
java.sql.SQLException: Incorrect DATETIME value: '+1000000000-01-01 00:00:00'
二、问题分析及解决
分析:
这个异常表明在执行数据库查询时出现了错误,原因是使用了不正确的 DATETIME 值:‘+1000000000-01-01 00:00:00’。
这可能是由于在数据库查询中使用了不支持的日期格式,或者是由于数据存储格式与数据库预期格式不匹配导致的。
根据断点分析,发现以下情况:
直接使用LocalDateTime取当前时间,再使用with()方法获取当天的最小时间,发现year超界了
使用LocalDate取当前时间,再使用with()方法获取当天的最小时间,发现year一样也超界了
使用LocalDate取当前时间,分别使用of()方法和with()方法获取当天最小时间,发现of()没有越界,with()依旧越界
解决方案:
经过多次调试,发现先创建LocalDateTime形式的时间对象,再去使用with()方法转换成当天最小或最大时间,就会出现数据超界,
只要先创建LocalDate类型对象,再使用LocaDateTime.of()方法获取最大最小时间,就可以避免这一问题了!
三、总结
任何疑问,欢迎私信指教
分享:
及至始皇,奋六世之余烈,振长策而御宇内,
吞二周而亡诸侯,履至尊而制六合,执敲扑而鞭笞天下,威振四海。