java.sql.SQLException: Value ”0000-00-00 00:00:00“ can not be represented as java.sql.Timestamp

java.sql.SQLException: Value ‘0000-00-00 00:00:00’ can not be represented as java.sql.Timestamp

  • 分析:

今天在项目中涉及到需要查询表中数据的时候,因为表中含有"CREATE_TIME字段",导致在select语句查询时报错。
  • 原因:

    • 1.首先,在创建表的时候,字段CREATE_TIME"格式为:

    ‘CREATE_TIME’ timestamp NOT NULL DEFAULT ‘0000-00-00 00:00:00’

    • 2.因为字段默认值设置,导致在新增数据的时候,不指定创建时间,create_time均为"0000-00-00 00:00:00"了
    • 3.而select语句,将此查询结果,内含create_time字段值"0000-00-00 00:00:00",再将其转换为Java的Timestamp类型的时候出错了。
    • 4.根本原因是:
Timestamp t = new Timestamp(System.currentTimeMillis())
System.out.println(t);
2020-06-29 16:57:43.663
可以明显的看出,java中的时间是从格林威治时间 1970年01月01日00时00分00秒开始的,这就导致"0000-00-00 00:00:00"Java根本无法表示此时间

解决

  • 1.大家普遍方法:在.xml中url路径中添加
<jdbcConnection userId="root" password="xxx" driverClass="com.mysql.jdbc.Driver" 
  connectionURL="jdbc:mysql://192.168.145.130:3306/test?zeroDateTimeBehavior=convertToNull"/>
或者
?zeroDateTimeBehavior=round
  • 2.第一种方式试了,没用。然后,将数据库表字段"CREATE_TIME"改成
 alter table 表名 modify CREATE_TIME timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间';

猜你喜欢

转载自blog.csdn.net/tmax52HZ/article/details/107024260