views:

633

answers:

3

Hello.

I want to store into the same table, in different columns 2 java.sql.Date objects to their corresponding SQL Datetime. It works fine, but the hour, minute and seconds are truncated. That's documented in the javadoc.

For instance having the date: 5/30/2009 14:33:21 AM when inserted in table it is truncated to 5/30/2009 12:00:00 AM .

I've also tried to store it as a Timestamp, but I cannot have 2 rows with Timestamp format into the same table.

Is there a workaround or something ?

+1  A: 

Why can't you have more than one row with Timestamp format? I think you have to elaborate there (what's your ORM if any, and what's your database server).

Anyhow you can either split the datetime into java.sql.Date and java.sql.Time and have two or more of each or use java.sql.Timestamp, which is what doesn't lose the time information by design. So you really should look into why you can't have more than one Timestamp.

Vinko Vrsalovic
Thank's for answering. The reason I cannot have 2 Timestamp columns is that I'm using MS SQL Server 2005 which doesn't allow to have 2 Timestamp columns for same table.
Flueras Bogdan
A: 

For an SQL DATETIME column you should still use a java.sql.Timestamp and the statement.setTimestamp( int, java.sql.Timestamp ) method.

The JDBC driver Timestamp class should map to both SQL DATETIME and SQL TIMESTAMP columns.

Neal Maloney
yes, that worked for me.
Flueras Bogdan
A: 

If you are using Oracle. This article might help you:
http://www.oracle.com/technology/tech/java/sqlj_jdbc/htdocs/jdbc_faq.html#08_01

Vinnie