使用Mysql从数据库中检索时间戳数据

问题描述:

我有一个Java MVC模型。其中有一个已插入到DB中的DB值的时间戳。我已经在数据库中声明了数据类型为时间戳,现在问题是如果我尝试检索它显示空值&不能表示为java.sql.Timestamp中使用Mysql从数据库中检索时间戳数据

声明:

pstmt = con.prepareStatement("SELECT timestamp, FROM nid where id=?"); 
pstmt.setDouble(1, nidev); 
      rs = pstmt.executeQuery(); 
      if(rs.next()) 
      { 
       timeBean.setHbtimestamp(rs.getTimestamp("timestamp")); 

      } 

Bean类:

private Timestamp hbtimestamp; 

public Timestamp getHbtimestamp() { 
    return hbtimestamp; 
} 
public void setHbtimestamp(Timestamp hbtimestamp) { 
    this.hbtimestamp = hbtimestamp; 
} 

MYDB值被成功插入:2015年5月14日15时45分57秒

输出:值 '0000-00-00 00:00:00' 不能被表示为java.sql中。时间戳

+0

“Hbtimestamp”的类型是什么类型的代码 –

+0

在设置之前,首先打印'rs.getTimestamp(“timestamp”)'的值。 –

+0

数据类型是时间戳 – Ramesh

设置在你的MySQL

zeroDateTimeBehavior=convertToNull 

然后空的时间戳值将被解释为空值

很好地解释here

+0

对不起,我没有得到,你能帮我吗 – Ramesh

+0

我做了这一个添加到URL&它被转换为空 – Ramesh

+0

jdbc:mysql:// localhost:3306/acc_dev_db?zeroDateTimeBehavior = convertToNull – Ramesh