• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 173
  • Last Modified:

Adding date to mysql tables?

Hello,

Using mysql I set a column datestamp to use the date field type.

Could anyone advise if I would use the java.sql.Date object to capture the time-clock to add this in the table?

And having stored the date, would I be-able to query the database datestamp field using a PreparedStatement to return fields with a particular month and year.

Or to query in this way would I need to have a separate, month and year field in the table?

Thank you

0
AntoniRyszard
Asked:
AntoniRyszard
  • 4
  • 2
1 Solution
 
CEHJCommented:
Yes, yes and no ;-)
0
 
CEHJCommented:
Bear in mind that sql Date contains no hours, minutes etc.
0
 
AntoniRyszardAuthor Commented:
Thanks

Could I ask if this seems the correct way of returning the day, month, year.

Calendar calendar = new GregorianCalendar(2006, 0, 1);
java.sql.Date date = new java.sql.Date(calendar.getTime().getTime());
 
And to add the date in a preparedstatement, would I need to use date.toString()?

0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
AntoniRyszardAuthor Commented:
Could I ask finally,

To return all the records with from march 06 and in ordered on the day. Could we achieve this from the datestamp field, which contains a java.sql.Data value?
0
 
CEHJCommented:
>>Could I ask if this seems the correct way of returning the day, month, year.

That's fine

>>And to add the date in a preparedstatement, would I need to use date.toString()?

No, you'd use ps.setDate(date);

>>Could we achieve this from the datestamp field, which contains a java.sql.Data value?
Yes

String sql = "select x , date_col from y where date_col between ? and ? order by date_col";

ps.setDate(1, date);
ps.setDate(2, new java.sql.Date(System.currentTimeMillis()));

0
 
CEHJCommented:
:-)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now