【问题标题】:Converting string to SQL date type将字符串转换为 SQL 日期类型
【发布时间】:2012-06-18 08:47:50
【问题描述】:

我有一个作为文本字段的开始时间。我需要将我在文本字段中输入的字符串转换为 SQL 日期并存储到数据库中。

默认.xhtml

 Start Date 
 <h:inputText id="startdate" value="#{customer.start_date}" 
                size="20" required="true"
                label="startdate" >
            </h:inputText>

CustomerBean.java

      formatter = new SimpleDateFormat("dd/MM/yyyy");
      Date startDate = (Date)formatter.parse(CustomerBean.this.getStart_date());
      Date endDate = (Date)formatter.parse(CustomerBean.this.getEnd_date());
      @SuppressWarnings("deprecation")
    java.sql.Date startdate=new java.sql.Date(startDate.getDate());
      @SuppressWarnings("deprecation")
    java.sql.Date enddate=new java.sql.Date(endDate.getDate());
     System.out.println("Today is " +date );
     PreparedStatement ps 
        = con.prepareStatement(sql);
    ps.setDate(1,startdate);
    ps.setDate(2,enddate);

如果我运行这个程序,我会收到以下错误。

An Error Occurred:

  java.text.ParseException: Unparseable date: "19/06/2012"
 - Stack Trace

  javax.faces.el.EvaluationException: java.text.ParseException: Unparseable date:    "19/06/2012"
at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:102)
at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
at javax.faces.component.UICommand.broadcast(UICommand.java:315)
at javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
at javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
at com.sun.faces.lifecycle.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
at com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
at com.sun.faces.lifecycle.LifecycleImpl.execute(LifecycleImpl.java:118)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:166)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)

【问题讨论】:

    标签: mysql jsf date jdbc


    【解决方案1】:

    你需要在模型类中独占使用java.util.Date,在DAO类中独占使用java.sql.Date。在视图中,您应该使用 &lt;f:convertDateTime&gt; 在 HTML/HTTP 中的 String 表示和模型类中的 java.util.Date 之间进行转换。

    型号:

    import java.util.Date;
    
    // ...
    
    private Date startDate;
    private Date endDate;
    

    查看:

    <h:inputText value="#{customer.startDate}">
        <f:convertDateTime pattern="dd/MM/yyyy" />
    </h:inputText>
    <h:inputText value="#{customer.endDate}">
        <f:convertDateTime pattern="dd/MM/yyyy" />
    </h:inputText>
    

    道:

    import java.sql.Date;
    
    // ...
    
    preparedStatement.setDate(1, new Date(customer.getStartDate().getTime()));
    preparedStatement.setDate(2, new Date(customer.getEndDate().getTime()));
    

    【讨论】:

      猜你喜欢
      • 2021-04-10
      • 2013-07-12
      • 2018-03-03
      • 1970-01-01
      • 2020-07-25
      • 2011-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多