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

  • how to insert current date and time into DB2

    Hi
    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="com.foo.dao">
    <class name="RespVO" table="foo_response" dynamic-update="true">
    <id name="id" column="id" type="java.lang.Integer" unsaved-value="null">
    <generator class="native"/>
    </id>

    <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" />

    </class>
    </hibernate-mapping>


    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) {
    this.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);
    logger.info("Context URL : " + ctx.getDisplayName());
    dao = (ResponseDAO) ctx.getBean("RespDAO");
    record = new RespVO();
    //set the VO values
    record.setfReqId(Integer.valueOf(ID));

    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());
    record.setrespDate(sqlDate);
    //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());
    record.setrespTime(null);
    dao.saveOrUpdate(record);

    }



    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.?

    Thanks
    AD

  • #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.

    Comment


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

      Comment


      • #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
        Code:
        java.sql.Date sqlDate = new java.sql.Date(new Date().getTime());
        record.setrespDate(sqlDate);
        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: [com.foo.dao.RespVO] [insert into response (id, REQUESTID, RESPDATE, RESPTIME) values (default, ?, ?, ?)]
        COM.ibm.db2.jdbc.DB2Exception: [IBM][CLI Driver] CLI0102E Invalid conversion. SQLSTATE=07006
        Last edited by akdasari; Jun 11th, 2007, 06:35 AM.

        Comment


        • #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.

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

          Comment


          • #6
            Hi
            i tried all those changing from sql date to util date no use.
            now my VO date is java.util.date
            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
            COM.ibm.db2.jdbc.DB2Exception: [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?

            AD

            Comment


            • #7
              hi mdeinum

              Thanks for the info it works now.

              AD

              Comment

              Working...
              X