This week's book giveaway is in the Jython/Python forum.
We're giving away four copies of Murach's Python Programming and have Michael Urban and Joel Murach on-line!
See this thread for details.
Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How to insert date into Database from JSF table (dateformat problem)  RSS feed

 
Woko Meier
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

I'm developing a relatively simple CRUD application based on JSF 1.2 and would like to allow users input calendar dates into the Database.
However I'm encountering problems with date formats and am unable to convert a date inputted manually or by a calendar component into the correct format for MS SQL Server.

TABLE
The page concerned consists of a Woodstock table component : <webuijsf:table ...>

ROWGROUP
That table makes use of a CachedRowSet : <webuijsf:tableRowGroup binding="#{myPage.tableRowGroup1}" id="tableRowGroup1" sourceData="#{myPage.myDataProvider}" sourceVar="currentRow" ...>

COLUMNS
TableColumns hold textfields allowing for user input such as the following
<webuijsf:textField id="textField22" text="#{currentRow.value['Name']}"/>

SAVING BACK TO DB
In order to save data back to the Database there is a submit button committing the transaction : myDataProvider.Commit()
This works fine for any datatypes other than dates.

PROBLEM WITH ENTERING DATES
Trying to use the following text field for a date
<webuijsf:textField id="textField23" text="#{currentRow.value['myDate']}"/>

... and entering a value such as "12/31/09"results in the following error message
'12/31/09' could not be understood as a date and time.

The only compatible date format seems to be "Dec 31, 2009 12:00:00 AM"
I tried using converters (SQLTimestampConverter, SQLDateConverter) but they did not help

TARGET SOLUTION: Calendar component
Eventually I would like to use a popup calendar component such as <webuijsf:calendar> or Tomahawk <t:inputCalendar>

Please advice!
Thanks
Woko



*********************
JSF 1.2
MS SQL 2005
The dateformat on MS SQL Server are configured as [smalldatetime]
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think this is because of the almost total lack of sanity in SQL reqarding date and time formats. It's not unlikely that you need to format your date as a 'YYYY-MM-DD' string or something like that. Check the SQL Server docs for their particular formats.

You'll have the greatest amounts of problems with this kind of stuff if you are building up SQL by brute force. You'll have less of a problem when you use Prepared Statements using the "setDate()" method, and still fewer ones when using an ORM system such as Hibernate.

Another problem when working with Java, dates, and databases is that date/time values are a lot like floating-point. You have to be very careful to observe the precision with which the date/time values are stored. Different databases have different types of date and time (and date-time) formats that they store data in, and the granularity varies from whole days to seconds to 10th's of seconds to miili- or microseconds. This can cause problems when you attempt to fetch items based on exact values of date/time and why timestamps are particularly bad as primary keys.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!