wood burning stoves 2.0*
The moose likes JDBC and the fly likes Inserting date in MySQL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Inserting date in MySQL" Watch "Inserting date in MySQL" New topic
Author

Inserting date in MySQL

Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi ranchers,
I try to insert some dates in MySQL from the Java code but i fail to insert it in proper format. The default format of MySQL is "yyyy-MM-dd". So i set this as my default too. But when i insert the date it changes like this.. "0029-22-08"..Where '08 is the year field... Why this happens.. How could i change that to its proper default format... And here is my code


: Edited :


Never try to be a hard-worker. Be a smart-worker.
My Blog
Herman Schelti
Ranch Hand

Joined: Jul 17, 2006
Posts: 387
hi Rajkumar,

this is what I understand from your problem.

Your inputdate is a String, in the dd-MM-yyyy format, and you want to create a Date object, right?

Here's an example:


hope this helps,
Herman
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

So you have some code that does nothing, then you have some code which uses Strings that aren't in the right format for java.sql.Date. Here's better code:
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

How that'd be false... I just parse the date and store it in d1 and then format it to the required format and transfer it to date variable. Is this wrong? Just for clarification only i ask this!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Originally posted by Rajkumar balakrishnan:
How that'd be false... I just parse the date and store it in d1 and then format it to the required format and transfer it to date variable. Is this wrong? Just for clarification only i ask this!!
You parsed the date string and then formatted it to the format it was originally stored in. Now if you had formatted it to the required format -- which you didn't -- that would have worked. It would have been clumsy but it would have worked.

Note that your preferred way is like this:

String -> java.util.Date -> String -> java.sql.Date

Here there are three conversions between Strings and internal formats. My preferred way is like this:

String -> java.util.Date -> java.sql.Date

Here there is one conversion betwen Strings and internal formats. And also, you don't have to know which date format java.sql.Date's constructor requires.
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Thanks Paul for clarifying my doubt and probably i might get some good result with the code...
Rajkumar balakrishnan
Ranch Hand

Joined: May 29, 2008
Posts: 445

Hi friends,

i got a clear definition with my problem regarding storing date in mysql... and i now retrieve the date using Preparedstatement's getDate() method but it shows date by a default format like 'yyyy/MM/dd'.. But i want it to display in 'dd/MM/yyyy' format... How could i achieve it..Please guide me....
 
Consider Paul's rocket mass heater.
 
subject: Inserting date in MySQL