H2时间戳 - 始终插入(或获取)当前日期

时间:2017-03-17 05:17:02

标签: mysql sql spring spring-boot h2

我是SpringBoot的新手,我使用嵌入式数据库H2创建了一个应用程序。

CREATE TABLE IF NOT EXISTS t_occurrence (
   id     bigint PRIMARY KEY,
   where  varchar(100),
   when   timestamp null
);

我使用这个类来插入值,但无论我插入什么,我总是得到当前日期occurrence.getWhen()

int numOfRowsAffected = jdbcTemplate.update(
    "insert into t_occurrence (ID, where, when) " 
    + " values (?,?,?);",
    id,
    occurrence.getWhere(),
    occurrence.getWhen()
);

if (numOfRowsAffected==1) return id;
else return -1;

甚至硬编码时间

int numOfRowsAffected = jdbcTemplate.update(
        "insert into t_occurrence (ID, where, when) " 
        + " values (?,?,''2012-09-17 18:47:52.69');",
        id,
        occurrence.getWhere(),
        occurrence.getWhen()
    );

    if (numOfRowsAffected==1) return id;
    else return -1;



public class Occurrence {



private Long id;


private Date when;


private String where;



public Long getId() {

    return id;

}



public void setId(Long id) {

   this.id = id;

}


public Date getWhen() {

   return when;

}



public void setWhen(Date when) {

this.when = when;

}



public String getWhere() {

return where;

}



public void setWhere(String where) {

this.where = where;

}

}

1 个答案:

答案 0 :(得分:0)

当不使用“timestamp”时,您应该使用“日期”类型和“时间”类型

“timestamp”的类型是服务器

的自动通用