Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: How to insert date in database

  1. #1
    Junior Member
    Join Date
    Aug 2011
    Posts
    9
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default How to insert date in database

    HI Everybody,
    I'm creating registration form but getting problem, need your help guys.
    I've create table in MS SQL in that birthdate column data type i'd given datetime.
    While inserting data in table except this column everything is inserted.Only becoz this column is not
    varchar.
    Can anyone tell how to insert values in that column through JSP page.
    I am using preparedstmt to insert all column except this one.When i try so i get string to date conversion error.
    pls guys help me !!

    Thanking you .


  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How to insert date in database

    DateFormat is an abstract class for date/time formatting subclasses which formats and parses dates or time in a language-independent manner. The date/time formatting subclass, such as SimpleDateFormat, allows for formatting (i.e., date -> text), parsing (text -> date), and normalization. The date is represented as a Date object or as the milliseconds since January 1, 1970, 00:00:00 GMT.


    DateFormat provides many class methods for obtaining default date/time formatters based on the default or a given locale and a number of formatting styles. The formatting styles include FULL, LONG, MEDIUM, and SHORT. More detail and examples of using these styles are provided in the method descriptions.

    To format a date for the current Locale, use one of the static factory methods:

    myString = DateFormat.getDateInstance().format(myDate);

    If you are formatting multiple dates, it is more efficient to get the format and use it multiple times so that the system doesn't have to fetch the information about the local language and country conventions multiple times.

    DateFormat df = DateFormat.getDateInstance();
    for (int i = 0; i < myDate.length; ++i) {
    output.println(df.format(myDate[i]) + "; ");
    }

    To format a date for a different Locale, specify it in the call to getDateInstance().

    DateFormat df = DateFormat.getDateInstance(DateFormat.LONG, Locale.FRANCE);

    You can use a DateFormat to parse also.

    myDate = df.parse(myString);


    SO In your case , Change the String to the date using DateFormat than insert this value to the date type defined column ....

    I think this might be helpfull to you.
    Last edited by Manoj Kumar Chhetri; January 24th, 2012 at 03:29 PM.

Similar Threads

  1. Replies: 1
    Last Post: July 22nd, 2011, 07:08 AM
  2. [SOLVED] Inserting date into database
    By mithcool in forum Java Theory & Questions
    Replies: 2
    Last Post: July 16th, 2011, 01:54 AM
  3. [SOLVED] Using Java CheckBox to insert data to MSAccess Database
    By sandlucky in forum JDBC & Databases
    Replies: 1
    Last Post: May 15th, 2011, 11:43 PM
  4. Insert into database from flat file
    By HelloAll in forum JDBC & Databases
    Replies: 2
    Last Post: March 10th, 2011, 11:05 PM
  5. Replies: 1
    Last Post: January 15th, 2010, 01:32 AM