how to insert current date and time to db

Discussion in 'Java' started by Mullin, Apr 8, 2005.

  1. Mullin

    Mullin Guest

    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);
     
    Mullin, Apr 8, 2005
    #1
    1. Advertising

  2. Mullin

    Wannabee Guest

    "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
    ?
     
    Wannabee, Apr 8, 2005
    #2
    1. Advertising

  3. Mullin

    Wannabee Guest

    Actually it's java.sql.Timestamp, not TimeStamp !
     
    Wannabee, Apr 8, 2005
    #3
  4. Mullin

    shakah Guest

    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.
     
    shakah, Apr 8, 2005
    #4
  5. Mullin

    Guest

    I only use java dates, not sql dates, and store the long getTime()
    value directly in the database
     
    , Apr 9, 2005
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Chris Berg
    Replies:
    0
    Views:
    828
    Chris Berg
    Oct 27, 2003
  2. Peter Grison

    Date, date date date....

    Peter Grison, May 28, 2004, in forum: Java
    Replies:
    10
    Views:
    3,327
    Michael Borgwardt
    May 30, 2004
  3. Michael Hertz
    Replies:
    3
    Views:
    10,345
    Martin Honnen
    Oct 4, 2004
  4. Keith Cochrane
    Replies:
    2
    Views:
    635
    Keith Cochrane
    Aug 6, 2006
  5. Replies:
    1
    Views:
    5,887
Loading...

Share This Page