java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Date(示

Posted 雪山非猪

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Date(示相关的知识,希望对你有一定的参考价值。

架构使用jsp+servlet+java+mysql

mysql里time字段类型为datetime

java实体类中该字段类型为Date

页面中,时间字段类型为空的信息显示不出来,且报错信息如下:

java.sql.SQLException: Value \'0000-00-00 00:00:00\' can not be represented as java.sql.Date
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1056)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
	at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:927)
	at com.mysql.jdbc.ResultSet.getDateFromBytes(ResultSet.java:2304)
	at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:2052)
	at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:2015)
	at com.mysql.jdbc.ResultSet.getDate(ResultSet.java:2069)
	at dao.impl.DevicesDaoImpl.findAll(DevicesDaoImpl.java:26)
	at service.impl.DevicesServiceImpl.findAll(DevicesServiceImpl.java:20)
	at servlet.DevicesList.doGet(DevicesList.java:31)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:624)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at filter.EncodingFilter.doFilter(EncodingFilter.java:29)

 检查代码,并没有发现什么问题,只是因为这一条数据中,这个时间字段为空而已,而数据库中此字段是可以为空的

很显然,搜了一大堆没用的文章出来。也尝试改了很多。例如,在jsp页面<%=new SimpleDateFormat("yyyy-HH-mm").format(mobile.getMarketTime())%>

最终报错信息改成这样了:

java.lang.NullPointerException
	at java.util.Calendar.setTime(Unknown Source)
	at java.text.SimpleDateFormat.format(Unknown Source)
	at java.text.SimpleDateFormat.format(Unknown Source)
	at java.text.DateFormat.format(Unknown Source)
	at org.apache.jsp.info_jsp._jspService(info_jsp.java:200)
	at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:439)
	at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:395)
	at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:339)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:731)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
	at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:747)
	at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:485)
	at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:410)
	at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:337)
	at servlet.DevicesList.doGet(DevicesList.java:33)

 不放弃,继续找

http://blog.csdn.net/dczjzz/article/details/52446901 感谢作者,帮忙解决了问题,另外他出淤泥而不染,肯定是自己搞定的

解决了!

 

以上是关于java.sql.SQLException: Value '0000-00-00 00:00:00' can not be represented as java.sql.Date(示的主要内容,如果未能解决你的问题,请参考以下文章

Oracle DB:java.sql.SQLException:关闭连接

Cause: java.sql.SQLException: 请求的转换无效 ; uncategorized SQLException for SQL []; SQL state [99999]; er

java.sql.SQLException: ORA-00604: 递归 SQL 级别 1 发生错误

Java:Sqlexception:位置不支持的 SQL92 令牌:178

java.sql.SQLException:接近“on”:语法错误问题

java.sql.SQLException:架构“ROOT”不存在