Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Insert the current date

 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi friends,
I need to insert the current date in the MySQL table and when i try to do so a compilation error occurs..

: Edited :
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to insert a 'java.sql.Date' into 'ps.setDate(1, ...)'
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Larsen:
You need to insert a 'java.sql.Date' into 'ps.setDate(1, ...)'


I want to insert the current date into table, but i don't know how to accomplish that.. Please post me some code here..I am in :roll:
Please help me..........
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajkumar balakrishnan:


I want to insert the current date into table, but i don't know how to accomplish that.. Please post me some code here..I am in
Please help me..........







Thanks,
Shailesh
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shailesh Chandra:







Thanks,
Shailesh


Thanks for that... And now a silly question is this one stores the time in milliseconds where i need it to be stored in yyyy-MM-dd format so that i can able to view it in reports.... Please help me regards this
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajkumar balakrishnan:


Thanks for that... And now a silly question is this one stores the time in milliseconds where i need it to be stored in yyyy-MM-dd format so that i can able to view it in reports.... Please help me regards this


If you need to show the date in a special format, you need to use SimpleDateFormat to format the date/timestamp returned fra the database.
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Larsen:


If you need to show the date in a special format, you need to use SimpleDateFormat to format the date/timestamp returned fra the database.


Is there anyway to store the current date in the specified format...???
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajkumar balakrishnan:


Is there anyway to store the current date in the specified format...???


Yes as a String (varchar).
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
: Edited :
 
Rene Larsen
Ranch Hand
Posts: 1179
Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To be able to store the date as a String (varchar) in the database, you need to verify that the data type for the 'date' field is a varchar - or you have to change it from date/timestamp to varchar.

Here is how you put a Date as a String into the database:
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rene Larsen:
To be able to store the date as a String (varchar) in the database, you need to verify that the data type for the 'date' field is a varchar - or you have to change it from date/timestamp to varchar.

Here is how you put a Date as a String into the database:


I dont want to change the field from date to varchar or something else.. So please help me inserting the current date with the required formatting....
 
Shailesh Chandra
Ranch Hand
Posts: 1082
Java Oracle Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Rajkumar balakrishnan:

I don't want to change the field from date to varchar or something else..


You should not! a date field should be of date type only in database



So please help me inserting the current date with the required formatting....


Why are you so specific about format, A date is stored in database as Date object not with its format. A format is only representation of date as string.

I suggest let the date be stored as Date and if you want to display same in a specific format is as Rene has suggested above. That would be an ideal approach.

Thanks,
Shailesh
 
Rajkumar balakrishnan
Ranch Hand
Posts: 445
Android Eclipse IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Shailesh Chandra:


Why are you so specific about format, A date is stored in database as Date object not with its format. A format is only representation of date as string.

I suggest let the date be stored as Date and if you want to display same in a specific format is as Rene has suggested above. That would be an ideal approach.

Thanks,
Shailesh


I have done it by the instructions given by the Rene..... Thanks ranchers for helping me...



Real life-savers.....
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic