将字符串转换为SQL日期类型 [英] Converting string to SQL date type
问题描述
Default.xhtml
开始日期
< h:inputText id =startdatevalue =#{customer.start_date}
size =20required = 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(今天是+日期);
PreparedStatement ps
= con.prepareStatement(sql);
ps.setDate(1,startdate);
ps.setDate(2,enddate);
如果我运行这个程序,我收到以下错误。
发生错误:
java.text.ParseException:不可抛弃的日期:19/06/2012
- 堆栈跟踪
javax.faces.el.EvaluationException:java.text.ParseException:Unparseable date:19/06/2012
javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java :102)
在com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
在javax.faces.component.UICommand.broadcast(UICommand.java:315)
在javax.faces.component.UIViewRoot.broadcastEvents(UIViewRoot.java:794)
在javax.faces.component.UIViewRoot.processApplication(UIViewRoot.java:1259)
在com.sun.faces。生命周期.InvokeApplicationPhase.execute(InvokeApplicationPhase.java:81)
在com.sun.faces.lifecycle.Phase.doPhase(Phase.java:101)
在com.sun.faces.lifecycle.LifecycleImpl。执行(Lifecyc leImpl.java:118)
在javax.faces.webapp.FacesServlet.service(FacesServlet.java:593)
在org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
在org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
在org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
在org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
在org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
在org.apache .catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
在org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
在org.apache.catalina.valves .AccessLogValve.invoke(AccessLogValve.java:562)
在org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
在org.apache.catalina.connector.CoyoteAdapter.service (CoyoteAdapter.java:395)
在org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:188)
在org.apache.coyote.http11.Http11Protocol $ Http11ConnectionHandler.process(Http11Protocol.java:166)
在org.apache.tomcat.util.net.JIoEndpoint $ SocketProcessor.run(JIoEndpoint.java:302)
在java.util.concurrent.ThreadPoolExecutor $ Worker.runTask(ThreadPoolExecutor.java:886)
在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:908)
at java.lang.Thread.run(Thread.java:619)
您需要使用 java.util.Date
专门在模型类中使用 java.sql.Date
专门在DAO类中。在该视图中,您应该使用 < 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()));
I have a starttime as textfield. I need to covert the String which I type in the text field to SQL date and store into database.
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);
If I run this program I am getting the following error.
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)
You need to use java.util.Date
exclusively in the model class and use java.sql.Date
exclusively in the DAO class. In the view you should use <f:convertDateTime>
to convert between the String
representation in HTML/HTTP and the java.util.Date
in the model class.
Model:
import java.util.Date;
// ...
private Date startDate;
private Date endDate;
View:
<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()));
这篇关于将字符串转换为SQL日期类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!