util.date 转化为 sql.date

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/zhaihao1996/article/details/89320388

由于Java中一般date的格式为 util的,而数据库的date格式一般为sql的

利用jdbc向数据库插入date数据的时候,由于sql.date是util.date的子类,不可以直接插入,所以要转化一下.

转化方式有两种

第一种 将utildate 转化为毫秒数 然后再利用sqldate将毫秒数转化为sqldate

第二种 将utildate转化为指定格式的字符串 然后利用sql语句将字符串传入数据库

//第一种方式
//建立utilDate对象
java.util.Date utilDate = new java.util.Date();

//获取当前时间转化为sql对象 此时就可以传入数据库 格式为yyyy-MM-dd
java.sql.Date sqlDate = new java.sql.Date(utilDate.getTime());//这里是获取当前时间date.getTime()

//sq语句为 
insert into Users values(to_date('sqlDate','yyyy-MM-dd'))


//第二种方式
//利用SimpleDateFormat设定格式
SimpleDateFormat f = new SimpleDateFormat("yyyy-MM-dd HH24:mi:ss");

//建立utilDate对象
java.util.Date utilDate = new java.util.Date();//这里自己设置时间

//将utildate转化为对应格式的String字符串 然后可以利用sql语句to_date传入数据库
String dd = f.format(utilDate);

//sq语句为 
insert into Users values(to_date('dd','yyyy-MM-dd HH24:mi:ss'));
//注意 这是使用格式为yyyy-MM-dd HH24:mi:ss 是因为oracle数据库在这方面比较苛刻 
//有时候识别yyyy-MM-dd HH:mm:ss会发生错误

 

展开阅读全文

sql.Date异常】

04-13

采用SSH框架rn先描述下功能:rn1.显示机会列表(其中包括各种字段,与问题有关的是创建时间createDate和分配时间dueDate)rn2.显示计划列表(同上,计划创建时间createDate)rnrn两个表本来有测试数据的,rnrn当我跳转到机会列表:rn 代码:rn public ActionForward toList(ActionMapping mapping, ActionForm form,rn HttpServletRequest request, HttpServletResponse response)rn throws Exceptionrn ChanceForm myForm = (ChanceForm)form;rn this.chanceBiz.search([color=#FF0000]myForm.getItem()[/color], myForm.getPageResult());rn return mapping.findForward("list");rn rn出现异常:rn控制台异常:rn严重: Servlet.service() for servlet action threw exceptionrnjava.lang.ClassCastException: java.sql.Timestamprn at cn.qdqn.crm.my.entity.SalChance$$BulkBeanByCGLIB$$8649f480.setPropertyValues()rn at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValuesWithOptimizer(PojoEntityTuplizer.java:212)rn at org.hibernate.tuple.PojoEntityTuplizer.setPropertyValues(PojoEntityTuplizer.java:185)rn at org.hibernate.persister.entity.AbstractEntityPersister.setPropertyValues(AbstractEntityPersister.java:3232)rn at org.hibernate.engine.TwoPhaseLoad.initializeEntity(TwoPhaseLoad.java:129)rn at org.hibernate.loader.Loader.initializeEntitiesAndCollections(Loader.java:842)rn at org.hibernate.loader.Loader.doQuery(Loader.java:717)rn at org.hibernate.loader.Loader.doQueryAndInitializeNonLazyCollections(Loader.java:224)rn at org.hibernate.loader.Loader.doList(Loader.java:2145)rn at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2029)rn at org.hibernate.loader.Loader.list(Loader.java:2024)rn at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:375)rn at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:308)rn at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:153)rn at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1106)rn at org.hibernate.impl.QueryImpl.list(QueryImpl.java:79)rn at cn.qdqn.crm.common.dao.impl.CommonDAOHibImpl.listByPage(CommonDAOHibImpl.java:64)rn at cn.qdqn.crm.my.biz.impl.ChanceBizImpl.search(ChanceBizImpl.java:110)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:304)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:182)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:149)rn at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:106)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)rn at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:89)rn at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:171)rn at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:204)rn at $Proxy9.search(Unknown Source)rn at cn.qdqn.crm.my.web.action.ChanceAction.toList(ChanceAction.java:83)rn at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)rn at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)rn at java.lang.reflect.Method.invoke(Unknown Source)rn at org.apache.struts.actions.DispatchAction.dispatchMethod(DispatchAction.java:270)rn at org.apache.struts.actions.DispatchAction.execute(DispatchAction.java:187)rn at cn.qdqn.crm.common.web.action.BaseAction.execute(BaseAction.java:49)rn at org.springframework.web.struts.DelegatingActionProxy.execute(DelegatingActionProxy.java:110)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:431)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)rn at org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)rn at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)rn at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)rn at java.lang.Thread.run(Unknown Source)rnrn页面异常:rnjavax.servlet.ServletException: org.hibernate.PropertyAccessException: exception setting property value with CGLIB (set hibernate.cglib.use_reflection_optimizer=false for more info) setter of cn.qdqn.crm.my.entity.SalChance.setChcDueDatern org.apache.struts.action.RequestProcessor.processException(RequestProcessor.java:535)rn org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:433)rn org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:236)rn org.apache.struts.action.ActionServlet.process(ActionServlet.java:1196)rn org.apache.struts.action.ActionServlet.doGet(ActionServlet.java:414)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:690)rn javax.servlet.http.HttpServlet.service(HttpServlet.java:803)rn org.springframework.orm.hibernate3.support.OpenSessionInViewFilter.doFilterInternal(OpenSessionInViewFilter.java:198)rn org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)rn org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:96)rn org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:75)rnrnrnrn--------------------------------------------------------rn当机会列表没有数据的时间反而可以正常显示,然后通过新建插入数据没有任何问题。。。rn当机会被指派则条数据在机会表删除,创建到计划表,rn当我转到计划列表的时候,因为里面有数据,类似上面的问题出现了。。。rnrnrn--------------------------------rnPS:rn 日期用的是sql.Date包rn 是不是struts在填充form的时候不认识我的测试数据?rn 但类型是匹配的。。。rnrn向各位请教。rnrn 论坛

没有更多推荐了,返回首页