开发者社区> 问答> 正文

时间为空值的设为0000-00-00怎么插入到数据库中

Java读取json数据到MySQL中,json中的regist_time为String ,数据库中的为Date,当regist_time为空时插入不到数据库,所有regist_time为空时,时间设为0000年00月00日,还是不对,
if(regist_time=="") regist_time="0000年00月00日";
SimpleDateFormat format = new SimpleDateFormat("yyyy年MM月dd日");
Date date =format.parse(regist_time);
java.sql.Date sqlDate=new java.sql.Date(date.getTime());

return sqlDate;

展开
收起
杨冬芳 2016-07-05 18:18:14 2953 0
1 条回答
写回答
取消 提交回答
  • IT从业

    设置为null试试呢?另外你数据库里面的字段要是可以为空的才行。 在使用mysql时,有时候会遇到插入空日期的方法,比如col_date这个列属性是日期,可以为空(null),这时候如果我们想直接插入空日期,可以直接这么写:insert into table table1 (col_date)values(null);当然也可以更新空日期:update table table1 set col_date = null where id = 1

    2019-07-17 19:51:46
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载