Whether you store nulls in your database is really a design decision you
should be making based on your requirements.
Taking the date example for a moment. If the date is required then it
shouldn't be able to be null in the database and a record shouldn't be
created until you have a valid date. However you might have optional
dates. In these cases you may use null to show that the date has not
been set yet. Setting the date to some funny value may mean you have to
jump through hoops to filter out these dates from queries in other parts
of the application.
I'll see if I can whip up an example...
How about a date that is used to record when some process has
completed. It will be called endDate. It can be null in the database
because the process has not finished yet.
Now a struts action is called to update the process. My struts action
talks to my application objects which do the database retrieval and
populate an object representing the process. The there will be some
methods such as Date getEndDate() and setEndDate(Date date) so I can
rad/write to the field.
Next, I need to transfer this to my form object.
Struts won't deal with Date objects on a web page, it needs to deal in
strings. So we will have String getEndDate() and setEndDate(String
date) on the form object. These will be used by our form fields for
display and populating when the form is submitted.
So the tricky thing is how to convert my date into a string and the
reverse when we want to read the date off the submitted form.
The easiest may be to tack some extra methods onto the form object eg
setEndDateAsDate(Date date). This takes the date object, which may be
null, and generates a human readable version using SimpleDateFormat for
example, or storing an empty string in the form object.
Same goes for retrieving the endDate from the form. We might add Date
getEndDateAsDate(). This method will check if the string is empty, if
so it returns null. If the string isn't a valid date it returns null.
If the string can be converted into a valid date using SimpleDateFormat,
the date object is returned.
You might also like to add an boolean isValidEndDate() method. If the
string returned is not empty, the string is checked for a valid date, if
it fails the user can be asked to reenter the date.
This means your application doesn't have to change deal with the
nulls/empty strings/incorrect dates are entered.
Murray Collingwood wrote:
Hi all
Just like to say I really appreciate the help you guys provide us newbies. I'm looking
forward to the day I will be able to help others...
What convention do people normally use for dealing with NULL values from an SQL
database?
I can retrieve them okay.
I can store them in my form class okay.
But when Struts tries to display them on a form I get an exception.
1. Do you test them at the SQL retrieve and set a displayable value? eg change a null
string to an empty string, change a null date to 00/00/0000
2. Do you handle them in the form class with different getters and setters? eg a different
property name for displaying them on the form
3. Do you handle them on the form somehow? (not sure how you would do this)
4. Or do you simply set your database so that the fields can't be null and give them
defaults, ie strings always default as empty strings?
Currently I'm tyring to use method 2, coding different property names in the jsp and
using the getter and setter methods to transfer the null value to something that can be
edited. I'm still working through some errors trying to process my date fields but
wondered whether I was actually heading down the right alley?
Kind regards
mc
FOCUS Computing
Mob: 0415 24 26 24
[EMAIL PROTECTED]
http://www.focus-computing.com.au
--
Jason Lea
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]