The moose likes JDBC and the fly likes Problem Reading/Writing Date Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Problem Reading/Writing Date" Watch "Problem Reading/Writing Date" New topic

Problem Reading/Writing Date

Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49
Hi all,
I'm having a few major issues with Date over jdbc.

i) when i read a date out from my mysql database it cast it into java.util.Date to store in an object(errors if i try to store as java.sql.Date). Later on i have major problems trying to compar this date object...

ii) the second probelm is writing date to the db through jdbc. I set the date from todays date, cast it to java.sql.Date but when i write it to the db. i get 00-00-0000 in the db

Obviosly there is somethign strange going on between java.util.Date and java.sql.Date. Any help would be appreciated.

Yongping Wang
Ranch Hand

Joined: May 15, 2001
Posts: 40
You better use Timestamp instead of Date.
Jeanne Boyarsky
internet detective

Joined: May 26, 2003
Posts: 30057

To expand on Yongping's comment:

- java.sql.Date is the date without any time value
- java.sql.Time is the time without any date value
- java.sql.Timestamp is both the date and time

[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Jamie Wool
Ranch Hand

Joined: May 03, 2004
Posts: 49
Does that mean you can't compare java.sql.Date fields properly? I need the application to work out whether an item is out of date, so the precision is in terms days.

Also how do you write dates or timestamps through jbbc, as at the moment I have to use a sql based function like now(), because i simply get nulls if i attempt to pass a date. What i've been doing converting a java.util.Date to java.sql.Date, and passing that date across in the jdbc code? Is this not correct.
I agree. Here's the link: http://aspose.com/file-tools
subject: Problem Reading/Writing Date
Similar Threads
preparedStatement.setDate and java.util.Date
convert a String into Date with SimpleDateFormat
Reading from SQL Server
java.sql.SQLException: [Microsoft][ODBC SQL Server Driver]Opti onal fature not implem
Save date to the database