標籤:
發這篇博文的題目可能無法直接表示內容,但是確實是java.sql.Date和java.util.Date.
今天在使用‘net.sf.json.JSONObject‘封裝json資料的時候,碰到很奇怪的問題,報出的異常,讓我開始覺得,不知道從哪個異常開始找解決方案,報錯如下:
一月 30, 2015 11:14:49 上午 org.apache.catalina.core.StandardWrapperValve invokeSEVERE: Servlet.service() for servlet [spt.servlet.JsonServlet] in context with path [/ServerAQI] threw exceptionnet.sf.json.JSONException: java.lang.reflect.InvocationTargetExceptionat net.sf.json.JSONObject._fromBean(JSONObject.java:953)at net.sf.json.JSONObject.fromObject(JSONObject.java:192)at net.sf.json.JSONObject._processValue(JSONObject.java:2774)at net.sf.json.JSONObject._setInternal(JSONObject.java:2798)at net.sf.json.JSONObject.setValue(JSONObject.java:1507)at net.sf.json.JSONObject._fromBean(JSONObject.java:940)at net.sf.json.JSONObject.fromObject(JSONObject.java:192)at net.sf.json.JSONObject._processValue(JSONObject.java:2774)at net.sf.json.JSONObject.processValue(JSONObject.java:2833)at net.sf.json.JSONObject.element(JSONObject.java:1871)at net.sf.json.JSONObject.element(JSONObject.java:1849)at net.sf.json.JSONObject.put(JSONObject.java:2466)at spt.service.JsonService.getJsonStr(JsonService.java:12)at spt.servlet.JsonServlet.doPost(JsonServlet.java:34)at spt.servlet.JsonServlet.doGet(JsonServlet.java:53)at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)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 spt.servlet.filter.EncodingFilter.doFilter(EncodingFilter.java:24)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.StandardWrapperValve.invoke(StandardWrapperValve.java:220)at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:503)at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1070)at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)at java.lang.Thread.run(Thread.java:722)Caused by: java.lang.reflect.InvocationTargetExceptionat sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)at java.lang.reflect.Method.invoke(Method.java:601)at org.apache.commons.beanutils.PropertyUtilsBean.invokeMethod(PropertyUtilsBean.java:1773)at org.apache.commons.beanutils.PropertyUtilsBean.getSimpleProperty(PropertyUtilsBean.java:1132)at org.apache.commons.beanutils.PropertyUtilsBean.getNestedProperty(PropertyUtilsBean.java:686)at org.apache.commons.beanutils.PropertyUtilsBean.getProperty(PropertyUtilsBean.java:715)at org.apache.commons.beanutils.PropertyUtils.getProperty(PropertyUtils.java:290)at net.sf.json.JSONObject._fromBean(JSONObject.java:928)... 40 moreCaused by: java.lang.IllegalArgumentExceptionat java.sql.Date.getHours(Date.java:182)... 50 more
通常,我們有兩種思路找解決方案:從我們自訂類中開始找錯.
這種思路是我在網上看教程視頻,一個講師說的,蠻實用的.但是一開始的時候,我並沒有這樣做(其實我應該這樣做).我先是查 ‘java.lang.IllegalArgumentException java.sql.Date.getHours),發現有個哥們寫的一大篇中文,但覺得他說的都不怎麼對.
又一次覺得 stackoverflow上的哥們真的很厲害.
我們知道,java.sql.Date是java.util.Date的子類,所以我們無需顯示將java.sql.Date轉換為java.util.Date.
在jdbc中,使用PreparedStatement往資料庫中儲存date的時候,我們需要將java.util.Date轉換為java.sql.Date.
java.sql.Date在資料庫中儲存的是不帶time部分的,所以將java.sql.Date轉換為java.util.Date的時候,應該使用 ResultSet..getTimestamp("currentDate"), 而不能讓其隱式轉換(ref:Converting java.sql.Date to java.util.Date)
當我使用ResultSet..getTimestamp("currentDate")將java.sql.Date轉換為java.util.Date的時候,就能將json資料正常封裝了.
java.sql.Date to java.util.Date