Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

[Urgent] validating Date field and saving into DB?

Thread view: 
viki.sanjeeva@gmail.com - 20 Dec 2005 15:47 GMT
Hi,

There is a date field in JSP form where user will enter date in
dd-mm-yyyy format. Now before saving into DB2, I want to validate the
date format against dd-mm-yyyy format and then save into DB2.

I've tried reading SimpeDateFormat and other date format classes, but
couldn't understand. It will be great if somebody tell the following
with code.

1. How to validate Date so that it should satisfy dd-mm-yyyy format?
2. How to insert validated Date in DB2? Where ps.setDate() comes into
picture?

Note: The date column in DB2 is of type "DATE".

Bye,
Viki.
ganesh - 20 Dec 2005 16:30 GMT
public class Validator {
               private static final String NEW_LINE = "<BR>";
    private static final String DATE_FORMAT = "yyy-MMM-dd";
    SimpleDateFormat SDF = new SimpleDateFormat(DATE_FORMAT);

                private List errorList = new ArrayList();

    // Defualt Constructor
    public Validator () {
        errorList = new ArrayList();
    }

/****************************************************************************
 *Description of getErrors Method:Returns the list.of error Messages
 *

 *

****************************************************************************/
 public List getErrors() {
   return errorList;
 }

/****************************************************************************
 *Description of printErrors Method:this method calls getErrors method
which
 *                                  gives List of Errors then they are
printed
 *                                  on Server Window
 *

******************************************************************************/

 public void printErrors() {
   List l = getErrors();
   if (l.size() == 0) {
     log.debug("There are no errors!!");
   } else {
     Iterator i = l.iterator();
     while (i.hasNext()) {
       log.debug(i.next());
     }
   }
 }

/****************************************************************************
  *Description of validate method:This method is used for validating
String
  *                               values which are Mandetory
  *                               Typically the developer should get
the value
  *                               from the container and then call
this method
  *                               to check if it present of not
(i.e.it is null
  *                               or blank.)if it finds value null
then it calls
  *                               formatAndAdd(String message) method

  *
  *@Param:
- *   value - Value in String format
  *   Message - If the value is not present then this message is added
to the
  *             errorList.
  *

****************************************************************************/
 public void validate (String value, String message) {

   if (value == null  || value.equals("") ) {
     formatAndAdd(message);
   }

 }

/****************************************************************************
  *Description of validateDate method:This method is used for
validating Date
  *                                   which is in String Format
  *                                   Typically the developer should
check get
  *                                   the value from the containerand
then call
  *                                   this method to check if the date
is in
  *                                   correct format or not.
  *
  *@Param:
  *   value - Date as a String Object
  *   Message - If the Date is not in correct format then this message
is added
  *             to the errorList.
  *

*****************************************************************************/
 public void validateDate (String value, String message) {

   try {
     Date d = SDF.parse(value);
     System.out.println("This is the Date: " + d);
   } catch (Exception e) {
     formatAndAdd(message);
   }

 }

/******************************************************************************
*Description of formatAndAdd method:this method is called by other
methods of
*                                   the same class for  formating the
message
*                                   and adding it errorList when
method finds
*                                   any Validation error in input
Parameter
*
*@Param
*   message:message to be added to error List
*

******************************************************************************/
 private void formatAndAdd(String message) {
   StringBuffer sb = new StringBuffer(NEW_LINE);
   sb.append(message);
   sb.append(NEW_LINE);
   if (!errorList.contains(message)) {
     errorList.add(sb.toString());
   }
 }

}

hope this will help
castillo.bryan@gmail.com - 21 Dec 2005 05:44 GMT
For validating the date, use SimpleDateFormat's parse method.
You should also set lenient to false.  An exception will be thrown if
it doesn't parse.

try {
 DateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
 // this is very important, otherwise 31-09-2005 would parse as
01-10-2005
 sdf.setLenient(false);
 java.util.Date dt = sdf.parse("01-01-2006");
}
catch (ParseException pe) {
 // invalid date format
}

To insert into DB2, get a database connection and prepare a statement,
and like you were guessing, use PreparedStatement.setDate(int pos,
java.sql.Date date);


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.