Results 1 to 10 of 10
  1. #1
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Date was not saved into database

    I insert JCalendar control in my net beans program. The problem is when save the control value into database it raises an exception. So how to solve this problem? please Help me..

    Exception is: Conversion failed when converting date and/or time from character string



    Thank you in advance

  2. #2
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default Re: Date was not saved into database

    What does your SQL statement look like? Sounds like you are missing the mask when you are doing the conversion.

  3. #3
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: Date was not saved into database

    Quote Originally Posted by sehudson View Post
    What does your SQL statement look like? Sounds like you are missing the mask when you are doing the conversion.
    Oho! Sorry! the sql statement is:
    st.executeUpdate("insert into emp_info values('"+eid.getText()+"','"+ename.getText()+"',' "+eaddress.getText()+"',"+Integer.parseInt(esal.ge tText())+",'"+edesg.getText()+"','"+edate_of_joini ng.getDate()+"','"+edate_of_ending.getDate()+"','" +edate_of_birth.getDate()+"',"+Long.parseLong(emob ile.getText())+")");

  4. #4
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default Re: Date was not saved into database

    Does your

    Java Code:
    edate_of_ending.getDate()
    method return a java.util.Date object?

  5. #5
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: Date was not saved into database

    Quote Originally Posted by sehudson View Post
    Does your

    Java Code:
    edate_of_ending.getDate()
    method return a java.util.Date object?
    yes. and its not saved into my database because it return util.Date. but JDateChooser control returns util.date. so how to solve this type of problem

  6. #6
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default Re: Date was not saved into database

    Using a PreparedStatement is probably a better solution, but I would:

    1)Convert the edate_of_ending to a String

    Java Code:
    DateFormat dateFormat= new SimpleDateFormat("MM/dd/yyyy HH:mm:ss"); 
    String edateAsString = dateFormat.format(edate_of_ending.getDate());
    2) Once you have the edate_of_ending as a String, you can pass it, along with the mask, into your sql statement as:

    Java Code:
    TO_DATE(edateAsString , 'MM/dd/yyyy HH:mm:ss')
    Same thing goes for your edate_of_joini ng.getDate()

    That is assuming that the column that you are trying to insert to in your database is of type DATE

  7. #7
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,596
    Rep Power
    5

    Default Re: Date was not saved into database

    The post history of chandrasekhar.melam says that he's using SQLServer. TO_DATE is an Oracle specific function isn't it?

    I'd go for the PreparedStatement only; that way you can hardly go wrong and just let the driver figure it out.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  8. #8
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default Re: Date was not saved into database

    You're probably right. I assumed he was using an Oracle database.

  9. #9
    Join Date
    Sep 2011
    Location
    India
    Posts
    67
    Rep Power
    0

    Default Re: Date was not saved into database

    Quote Originally Posted by sehudson View Post
    You're probably right. I assumed he was using an Oracle database.
    Nope... i'm using SQL SERVER. i insert ur code but the problem was not solved

  10. #10
    sehudson's Avatar
    sehudson is offline Senior Member
    Join Date
    Mar 2010
    Posts
    350
    Rep Power
    5

    Default Re: Date was not saved into database

    Give this a try to get your date to a string:

    Java Code:
    DateFormat dateFormat= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    String edateStartAsString= dateFormat.format(edate_of_ending.getDate());
    Then do your insert as you were originally doing it, just use edateAsString as the value, same thing for the other date.
    For example, lets say your table had 3 columns: name, edateStart, edateEnd

    Java Code:
    Insert into emp_info values('myname', edateStartAsString, edateEndAsString)
    The 2nd and 3rd values are the Date objects converted to Strings.

Similar Threads

  1. Convert date from database
    By karq in forum New To Java
    Replies: 5
    Last Post: 07-13-2010, 08:44 AM
  2. inserting date in oracle database
    By jackjosh_in in forum Advanced Java
    Replies: 6
    Last Post: 05-23-2010, 06:38 PM
  3. o get the missing date from database
    By javastuden in forum New To Java
    Replies: 0
    Last Post: 12-11-2009, 11:15 AM
  4. Problem when insert Date to database(SQL)
    By hungleon88 in forum JDBC
    Replies: 6
    Last Post: 04-29-2009, 03:53 PM
  5. Compare date input to database with current date
    By hungleon88 in forum Advanced Java
    Replies: 2
    Last Post: 11-25-2008, 08:10 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •