Announcement Announcement Module
No announcement yet.
how to insert current date and time into DB2 Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • how to insert current date and time into DB2

    I'm having trouble inserting current date and time fields into DB2
    I'm using spring hibernate
    here is my .hbm.xml

    <hibernate-mapping package="">
    <class name="RespVO" table="foo_response" dynamic-update="true">
    <id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
    <generator class="native"/>

    <property name="fReqId" column="REQUESTID" type="java.lang.Integer" not-null="true" />
    <property name="respDate" column="RESPDATE" type="java.util.Date" />
    <property name="respTime" column="RESPTIME" type="java.sql.Date" />


    my VO class is

    public class RespVO implements Serializable {

    /* Private Fields */
    private Integer id;

    private Integer fReqId;

    private Date respDate;

    private Time respTime;

    public Integer getfReqId() {
    return fReqId;

    public void setfReqId(Integer reqId) {
    fReqId = reqId;

    public Integer getId() {
    return id;

    public void setId(Integer id) { = id;

    public Date getrespDate() {
    return respDate;

    public void setrespDate(Date respDate) {
    this.respDate = respDate;

    public Time getrespTime() {
    return respTime;

    public void setrespTime(Time respTime) {
    this.respTime = respTime;


    in test class i populate the VO and persist.

    private void writeToRespTable(String ID){
    String[] paths = { "conf/applicationContext.xml" };
    ctx = new ClassPathXmlApplicationContext(paths);"Context URL : " + ctx.getDisplayName());
    dao = (ResponseDAO) ctx.getBean("RespDAO");
    record = new RespVO();
    //set the VO values

    Calendar cal = Calendar.getInstance();
    Date today = cal.getTime();
    SimpleDateFormat dateFmt =new SimpleDateFormat("yyyy-MM-dd");
    java.sql.Date sqlDate = java.sql.Date.valueOf(dateFmt.format(today));
    System.out.println("sql date = "+sqlDate);
    //java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
    //SimpleDateFormat timeFmt =new SimpleDateFormat("HH:mm:ss");
    //java.sql.Time now = java.sql.Time.valueOf(timeFmt.format(today));
    //System.out.println("Tiem now = "+now);
    //Time now = new Time(new Date().getTime());


    Can sombody help me as i need to insert current date in one field and current time in anotehr filed on DB2.
    Is there any other way.?


  • #2
    First when posting code use the [ code][ /code] tags, you make a lot of people happier because y our post become (more) readable.

    Simple create 2 date objects in your object, create a new instance of date put it in the 2 fields and that's it.


    • #3
      Just out of interest, why are you trying to split the data and time up?


      • #4
        If you see my code i just tried inserting with new Date object ,somehow it won't get converted intoo DB2.
        The reason having two fileds is my frontend system whihc is written different language and handles as Date and Time seperate fileds so i hav eto have two seperate info fileds.

        I'm trying this way
        java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
        i can see the binding like this in log file
        DEBUG - binding '1' to parameter: 1
        DEBUG - binding '2007-06-11 12:31:44' to parameter: 2
        DEBUG - binding null to parameter: 3

        but i get the eroor

        DEBUG - could not insert: [] [insert into response (id, REQUESTID, RESPDATE, RESPTIME) values (default, ?, ?, ?)] [IBM][CLI Driver] CLI0102E Invalid conversion. SQLSTATE=07006
        Last edited by akdasari; Jun 11th, 2007, 06:35 AM.


        • #5
          your mapping is also wrong, hibernate doesn't know what to do. Also I would set the Date object in your VO to a java.util.Date instead of a java.sql.Date.

          <property name="respDate" column="RESPDATE" type="date" />
          <property name="respTime" column="RESPTIME" type="time" />


          • #6
            i tried all those changing from sql date to util date no use.
            now my VO date is
            and i changed my hbm.xml as you said to just date
            now what i can see in debug it is parsed as

            DEBUG - binding '11 June 2007' to parameter: 2 but still the error
   [IBM][CLI Driver] CLI0102E Invalid conversion. SQLSTATE=07006

            as in my DB2 the date columns is of type DATE and time column is of type TIME does that mean do i need to conevrt while inserting?



            • #7
              hi mdeinum

              Thanks for the info it works now.