Unix Technical Forum

Full date value not being written via JDBC - any ideas?

This is a discussion on Full date value not being written via JDBC - any ideas? within the SQL Server forums, part of the Microsoft SQL Server category; --> Hi, I'm trying to write a java.sql.Date to a database and the minutes/ seconds etc. are not being written. ...


Go Back   Unix Technical Forum > Database Server Software > Microsoft SQL Server > SQL Server

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 03-01-2008, 01:17 PM
MackTheKnife
 
Posts: n/a
Default Full date value not being written via JDBC - any ideas?

Hi,
I'm trying to write a java.sql.Date to a database and the minutes/
seconds etc. are not being written. I've seen and tested many
examples
found via searches and still have come up with nothing. Here's what I
have:

java.sql.Date formation - recommended constructor forms:
java.sql.Date entryDttm = new
java.sql.Date(System.currentTimeMillis());
OR
java.sql.Date entryDttm = new java.sql.Date(new
java.util.Date().getTime());


// prepared statement insert
pstmt.setDate(1, entryDttm);
// what is written to database
2007-02-07 12:00:00.000


// what needs to be written
2007-02-08 09:37:25.793

The above date value is written when I insert using the SQL method
GETDATE()

The field is stored in a MS SQL Server 8.0 database and is defined as
a 'datatime' or 'smalldatetime' field.

How can I replicate the results of GETDATE() into 'datetime' and
'smalldatetime' fields in
a MS-SQL Server database (or is it possible?). Would a timestamp
datatype make more sense?

I'm using MS_SQL Server JDBC drivers.

Any advice would be GREATLY appreciated!

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 03-01-2008, 01:17 PM
Erland Sommarskog
 
Posts: n/a
Default Re: Full date value not being written via JDBC - any ideas?

MackTheKnife (wgblackmon@yahoo.com) writes:
> I'm trying to write a java.sql.Date to a database and the minutes/
> seconds etc. are not being written. I've seen and tested many
> examples
> found via searches and still have come up with nothing. Here's what I
> have:
>
> java.sql.Date formation - recommended constructor forms:
> java.sql.Date entryDttm = new
> java.sql.Date(System.currentTimeMillis());
> OR
> java.sql.Date entryDttm = new java.sql.Date(new
> java.util.Date().getTime());
>
>
> // prepared statement insert
> pstmt.setDate(1, entryDttm);
> // what is written to database
> 2007-02-07 12:00:00.000
>
>
> // what needs to be written
> 2007-02-08 09:37:25.793


Oh-oh. Since I don't know Java or JDBC, I don't have any idea of what
is going on. But use the Profiler to see what is sent to SQL Server.

> How can I replicate the results of GETDATE() into 'datetime' and
> 'smalldatetime' fields in
> a MS-SQL Server database (or is it possible?). Would a timestamp
> datatype make more sense?


No, timestamp would not make any sense at all. This is an 8-byte
value that is automatically updated everytime the row is updated.
It has no relation to date and time.


--
Erland Sommarskog, SQL Server MVP, esquel@sommarskog.se

Books Online for SQL Server 2005 at
http://www.microsoft.com/technet/pro...ads/books.mspx
Books Online for SQL Server 2000 at
http://www.microsoft.com/sql/prodinf...ons/books.mspx
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 03-01-2008, 01:17 PM
joeNOSPAM@BEA.com
 
Posts: n/a
Default Re: Full date value not being written via JDBC - any ideas?

On Feb 8, 2:32 pm, "MackTheKnife" <wgblack...@yahoo.com> wrote:
> Hi,
> I'm trying to write a java.sql.Date to a database and the minutes/
> seconds etc. are not being written. I've seen and tested many
> examples
> found via searches and still have come up with nothing. Here's what I
> have:
>
> java.sql.Date formation - recommended constructor forms:
> java.sql.Date entryDttm = new
> java.sql.Date(System.currentTimeMillis());
> OR
> java.sql.Date entryDttm = new java.sql.Date(new
> java.util.Date().getTime());
>
> // prepared statement insert
> pstmt.setDate(1, entryDttm);
> // what is written to database
> 2007-02-07 12:00:00.000
>
> // what needs to be written
> 2007-02-08 09:37:25.793
>
> The above date value is written when I insert using the SQL method
> GETDATE()
>
> The field is stored in a MS SQL Server 8.0 database and is defined as
> a 'datatime' or 'smalldatetime' field.
>
> How can I replicate the results of GETDATE() into 'datetime' and
> 'smalldatetime' fields in
> a MS-SQL Server database (or is it possible?). Would a timestamp
> datatype make more sense?
>
> I'm using MS_SQL Server JDBC drivers.
>
> Any advice would be GREATLY appreciated!


Hi. You should instead construct a java.sql.Timestamp and send it via
setTimestamp(). A java.sql.Date is a *date*. The time portion is
normalized
to zero.

Joe Weinstein at BEA Systems

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 01:21 PM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
www.UnixAdminTalk.com