Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / Databases / November 2005

Tip: Looking for answers? Try searching our database.

Java Timestamp in SQL

Thread view: 
anonieko@hotmail.com - 08 Nov 2005 10:40 GMT
// DO NOT DO THIS!
// BUG: loses time of day
preparedStatement.setDate(1, new java.sql.Date(date.getTime()));

// DO THIS
preparedStatement.setTimestamp(1, new
java.sql.Timestamp(date.getTime()));
// java.sql.Timestamp is not a date

// MORE INFO GO TO
http://www.thunderguy.com/semicolon/2003/08/14/java-sql-date-is-not-a-real-date/

// GET BACK timestamp from DB

//  result set

java.sql.Timestamp timestamp = resultSet.getTimestamp(1);
java.util.Date d = new java.util.Date(timestamp.getTime() +
                                     timestamp.getNanos() / 1000000

or

// Java 1.4+
java.util.Date d = resultSet.getTimestamp(1);
Bjorn Abelli - 08 Nov 2005 11:40 GMT
<anonieko@hotmail.com> wrote...

> // DO NOT DO THIS!
> // BUG: loses time of day
> preparedStatement.setDate(1, new java.sql.Date(date.getTime()));

It's no bug at all, though it's not *convenient* for us Java developers.

Instead it's quite logical, as java.sql.* pretty much conforms to...

  SQL!

Those databases that really conform to "standard" SQL *doesn't* have any
time information in their DATE-fields (hence a java.sql.Date), instead they
have it in TIME-fields (note there is also a java.sql.Time!).

And there's also the datatype TIMESTAMP in the standard form of SQL, to have
a combination of date and time (hence the java.sql.Timestamp!)...

Unfortunately there's plenty of db:s that *doesn't* conform to standard SQL,
such as Oracle, MS Access/Jet, SQL Server, etc...

For the fields that includes both date and time in those databases, the
*closest* thing to represent it is java.sql.Timestamp.

No bug...

// Bjorn A

P.S. No, I'm not shouting, it's a common practice
to use only upper-case letters in SQL... :-)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.