将字符串转换为SQL日期类型

时间:2012-06-18 08:47:51

标签: mysql jsf date jdbc

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

Default.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)

1 个答案:

答案 0 :(得分:2)

您需要在模型类中专门使用java.util.Date,并在DAO类中专门使用java.sql.Date。在视图中,您应使用<f:convertDateTime>在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>

DAO:

import java.sql.Date;

// ...

preparedStatement.setDate(1, new Date(customer.getStartDate().getTime()));
preparedStatement.setDate(2, new Date(customer.getEndDate().getTime()));