calender gives problem while storing date

UI Components for JSF
Post Reply
swapnil
Posts: 24
Joined: 03 Feb 2012, 09:45

25 Apr 2012, 09:38

Sir,
I use p:calender in dd/MM/yyyy format and want to store it in MySql as Date type in same dd/MM/yyyy format.But it gives me a parser exception and MySQL Data Truncation.
Do i need to use any date formatter to store it in Database.

.xhtml

Code: Select all

                                                                   <p:calendar widgetVar="leaseExpirationDateTo"
											onchange="validateDate(this);"
											onfocus="jQuery('invoice_date.hasDatepicker').mask('99/99/9999');"
											value="#{paymentbean.date}"
											navigator="true" pattern="dd/MM/yyyy"
											id="invoice_date">
										</p:calendar>
.JAVA

Code: Select all

SimpleDateFormat sdf=new SimpleDateFormat("dd/MM/yyyy");
			String sdate=sdf.format(date);
			System.out.println(sdate);
			SimpleDateFormat sdd=new SimpleDateFormat("dd/MM/yyyy");
			Date d= sdf.parse(sdate);
			System.out.println(d);
			con.setAutoCommit(false);
			
			System.out.println(date);
			//String sql="insert into payment_header(payment_no,payment_date,remarks) values('"+payment_no+"','"+date+"','"+remarks+"')";
			//stmt.executeUpdate(sql); 
			PreparedStatement pstmt=con.prepareStatement("insert into payment_header(payment_no,payment_date,remarks) values(?,?,?)");
			pstmt.setString(1, payment_no);
			pstmt.setDate(2,(java.sql.Date.valueOf(sdate)));
			pstmt.setString(3, remarks);
			pstmt.executeUpdate();

Primefaces version:3.1.RC1
JSf2.0
primefaces-extensions
IDE Eclipse Helios
Apache TomCat 6

smithh032772
Posts: 6144
Joined: 10 Sep 2011, 21:10

26 Apr 2012, 04:46

Thank God for JPA (Java Persistance API) and NetBeans tutorials! Click URL below, read/study/complete this tutorial and see how much easier it is to use JPA versus preparedstatements, etc...

http://netbeans.org/kb/docs/web/jsf20-crud.html

Also, there is no need for all this in your xhtml (below). Did you see all this in PrimeFaces showcase examples for p:calendar? JSF bean validation will validate the enduser-selected value when form is submitted, and h:messages can/will display a nice user-friendly error message if enduser-selected value fails validation.

Code: Select all

                              <p:calendar widgetVar="leaseExpirationDateTo"
                                 onchange="validateDate(this);"
                                 onfocus="jQuery('invoice_date.hasDatepicker').mask('99/99/9999');"
                                 value="#{paymentbean.date}"
                                 navigator="true" pattern="dd/MM/yyyy"
                                 id="invoice_date">
                              </p:calendar>
Howard

PrimeFaces 6.0, Extensions 6.0.0, Push (Atmosphere 2.4.0)
TomEE+ 1.7.4 (Tomcat 7.0.68), MyFaces Core 2.2.9, JDK8
JUEL 2.2.7 | OmniFaces | EclipseLink-JPA/Derby | Chrome

Java EE 6 Tutorial|NetBeans|Google|Stackoverflow|PrimeFaces|Apache

Post Reply

Return to “PrimeFaces”

  • Information
  • Who is online

    Users browsing this forum: No registered users and 41 guests