Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to insert current date and time to db

Reply
Thread Tools

how to insert current date and time to db

 
 
Mullin
Guest
Posts: n/a
 
      04-08-2005
hi,

i use the following code, but the DateTime field at mysql is
2005-04-08 00:00:00

i want to have the time too. where should i change

...
...
// Get the system date and time.
java.util.Date utilDate = new Date();
// Convert it to java.sql.Date
java.sql.Date date = new java.sql.Date(utilDate.getTime());
...
...
...
PreparedStatement stmt = connection.prepareStatement(sql);
stmt.setDate(1, date);
 
Reply With Quote
 
 
 
 
Wannabee
Guest
Posts: n/a
 
      04-08-2005

"Mullin" wrote
> hi,
>
> i use the following code, but the DateTime field at mysql is
> 2005-04-08 00:00:00


Sorry, I don't really know the types in MySql, but maybe DateTime is
compatible with java.sql.TimeStamp ?

So you might create a TimeStamp and try to set that to the PreparedStatement
?


 
Reply With Quote
 
 
 
 
Wannabee
Guest
Posts: n/a
 
      04-08-2005

Actually it's java.sql.Timestamp, not TimeStamp !


 
Reply With Quote
 
shakah
Guest
Posts: n/a
 
      04-08-2005
Mullin wrote:
> hi,
>
> i use the following code, but the DateTime field at mysql is
> 2005-04-08 00:00:00
>
> i want to have the time too. where should i change
>
> ..
> ..
> // Get the system date and time.
> java.util.Date utilDate = new Date();
> // Convert it to java.sql.Date
> java.sql.Date date = new java.sql.Date(utilDate.getTime());
> ..
> ..
> ..
> PreparedStatement stmt = connection.prepareStatement(sql);
> stmt.setDate(1, date);


You can sidestep the issue by using the database's current date/time,
perhaps something like:
PreparedStatement stmt = connection.prepareStatement(
" INSERT INTO tableA(colA, colB, colC)"
+ " VALUES(?, NOW(), ?)"
) ;
stmt.setString(1, "contents of colA") ;
stmt.setString(2, "contents of colC") ;
stmt.executeUpdate() ;

That's arguably better, as it avoids issues with client-side
differences in clock settings. Otherwise, in your example you can
probably do stmt.setTimestamp(1, new java.util.Date()), though I'm not
sure.

 
Reply With Quote
 
christopher@dailycrossword.com
Guest
Posts: n/a
 
      04-09-2005
I only use java dates, not sql dates, and store the long getTime()
value directly in the database

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Oracle insert current date and time teser3@hotmail.com Java 1 02-17-2010 01:16 AM
w3.org suggestion .. page, date, time and topic, date, time code (wish list). Keith Cochrane HTML 2 08-06-2006 06:57 AM
Insert current timestamp/date from XSLT stylesheet into a XML document ??? Michael Hertz XML 3 10-04-2004 11:28 AM
how to compare current date to a future date and validate it in ASP.NET James P. ASP .Net 7 07-13-2004 10:13 PM
Date, date date date.... Peter Grison Java 10 05-30-2004 01:20 PM



Advertisments