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

时间:2024-04-10 14:34:58

架构使用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

解决了!