Results 1 to 8 of 8
  1. #1
    krishnareddy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default how to insert datetime value into sqlserver dynamically

    I need to update datetime value into sql server2008 database table dynamically. I have created a date variable like


    java.util.Date date=new java.util.Date();
    java.sql.Date da=new java.sql.Date(date.getDate());

    ...
    ...
    ..
    ..some database code..
    ...
    ......
    ResultSet rs..Some code..
    /* I amsetting datetime value dynamically into database

    rs.setDate(da);

    it is updating currentdate but time set to 27-01-2012 00:00:00..so i could not getting current time..please help me inthis..how can i Set datetime value dynamically.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: how to insert datetime value into sqlserver dynamically

    Why are you updating via a result set?

    Anyway, if this is a DATETIME field then use updateTimestamp().
    Which of course begs the question...there isn't a setTime() method on ResultSet.

  3. #3
    HowardHyde is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: how to insert datetime value into sqlserver dynamically

    PreparedStatement.setDate() takes a java.sql.Date as its argument, which truncates the time component. So you need to use PreparedStatement.setTimestamp(). Unfortunately, java.sql.Timestamp doesn’t take a java.util.Date in any constructor, so you have to take one extra step of getting the long millisecond value from your java.util.Date object via getTime() to pass to the java.sql.Timestamp constructor:
    pStat.setTimestamp( 4, new java.sql.Timestamp(myDate.getTime()));
    If anyone has a simpler, more streamlined solution than this, I have an open mind.

  4. #4
    krishnareddy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: how to insert datetime value into sqlserver dynamically

    HowardHyde,

    Thanks for the reply..it is working..
    Last edited by krishnareddy; 01-28-2012 at 06:10 AM.

  5. #5
    krishnareddy is offline Member
    Join Date
    Jan 2012
    Posts
    9
    Rep Power
    0

    Default Re: how to insert datetime value into sqlserver dynamically

    Sorry Tolls, I am using PreparedStatement obj to insert date into the table...
    Last edited by krishnareddy; 01-28-2012 at 06:11 AM.

  6. #6
    HowardHyde is offline Member
    Join Date
    Jan 2012
    Posts
    12
    Rep Power
    0

    Default Re: how to insert datetime value into sqlserver dynamically

    Quote Originally Posted by krishnareddy View Post
    HowardHyde,

    Thanks for the reply..it is working..
    You're welcome Krishna Reddy :)

  7. #7
    mkarthik90 is offline Member
    Join Date
    Feb 2012
    Posts
    62
    Rep Power
    0

    Default Re: how to insert datetime value into sqlserver dynamically

    How about passing the date object of utility package as an argument to the constructor of sql.Date.

    For example
    java.util.Date today = new java.util.Date();
    java.sql.Date sqlToday = new java.sql.Date(today.getTime());
    ps.setString(1,sqlToday);

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,798
    Rep Power
    19

    Default Re: how to insert datetime value into sqlserver dynamically

    If the column in the database is a DATE or DATETIME or TIMESTAMP (or whatever the particular name that flavour of database gives it), then you should use the proper setter or getter.
    By using setString() you are foring Java to convert the Date to a String via toString, and then forcing the database to convert it back to a DATE (presuming the format is correct). If the format is incorrect (which is quite likely) you'll simply end up with an error from the DB.

Similar Threads

  1. Connecting to sqlserver from Java
    By pink123 in forum JDBC
    Replies: 3
    Last Post: 01-06-2012, 09:36 AM
  2. datetime??
    By pinar in forum New To Java
    Replies: 1
    Last Post: 10-26-2010, 11:17 PM
  3. how to print the from and to datetime
    By baktha.thalapathy in forum New To Java
    Replies: 1
    Last Post: 06-10-2010, 09:58 AM
  4. datetime on a jtextfield
    By devstarter in forum New To Java
    Replies: 1
    Last Post: 03-02-2010, 01:08 PM
  5. Replies: 3
    Last Post: 02-28-2009, 09:17 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •