• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 168
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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