我遇到了从java向我的数据库插入一些时间戳的问题。我正在从API收集天气预报,然后将值插入我的数据库。对于历史价值,它完全正常,没有错误或问题。
但是,当我尝试插入未来的值时,对于某些数据,它将使用当前时间和日期而不是设置值插入它。我已经尝试打印出所有最终陈述,但没有任何问题。有谁能看到这个问题?
String sqlCheck = "select * from weatherforecast where datetime = '"
+ timestamp + "' and stationID = " + stationID;
PreparedStatement p = conn.prepareStatement(sqlCheck);
ResultSet r = p.executeQuery();
if (r.next()){
String query = "update weatherforecast set precipitation = ? and temperature = ? and stationID = ? where datetime = ?";
PreparedStatement preparedStmt = conn.prepareStatement(query);
preparedStmt.setDouble (1, o.getRain());
preparedStmt.setDouble(2, o.getTemp());
preparedStmt.setInt(3, stationID);
preparedStmt.setTimestamp(4, timestamp);
preparedStmt.executeUpdate();
} else {
p1.setTimestamp(1, timestamp);
p1.setDouble(2, o.getRain());
p1.setDouble(3, o.getTemp());
p1.setInt(4, stationID);
p1.execute();
}
我还添加了一些thread.sleep(2000)来查看是否能解决问题,但没有运气。 我知道时间戳的格式是正确的,因为相同的代码用于过去的数据。它还为未来日期插入新数据完全正确:
'5800','2015-04-02 12:00:00','0','9','25'
'6204','2015-04-02 12:00:00','0','8','21'
'6356','2015-04-02 12:00:00','0','8','26'
'6386','2015-04-02 10:06:19','0','11','1'
'6396','2015-04-02 10:06:19','0','8','2'
'6422','2015-04-02 10:06:19','0','10','14'
'6447','2015-04-02 10:06:19','0','6','16'
这里底部四的日期时间值是'2015-04-02 09:00:00' (插入数据的时间是10:06:19)
提前致谢..!
修改
这是timestamp值的声明(API存储解释所需时间的方式因此if语句)
else if (o.getTime().equals("2100")) {
timestamp = Timestamp.valueOf(o.getDate() + " 21:00:00.0");
}
观察的日期部分以以下格式存储:
“date”:“2015-04-05”
编辑2:
Timestamp timestamp = null;
if (o.getTime().equals("300")) {
timestamp = Timestamp.valueOf(o.getDate() + " 03:00:00.0");
} else if (o.getTime().equals("0")) {
timestamp = Timestamp.valueOf(o.getDate() + " 00:00:00.0");
} else if (o.getTime().equals("600")) {
timestamp = Timestamp.valueOf(o.getDate() + " 06:00:00.0");
} else if (o.getTime().equals("900")) {
timestamp = Timestamp.valueOf(o.getDate() + " 09:00:00.0");
} else if (o.getTime().equals("1200")) {
timestamp = Timestamp.valueOf(o.getDate() + " 12:00:00.0");
} else if (o.getTime().equals("1500")) {
timestamp = Timestamp.valueOf(o.getDate() + " 15:00:00.0");
} else if (o.getTime().equals("1800")) {
timestamp = Timestamp.valueOf(o.getDate() + " 18:00:00.0");
} else if (o.getTime().equals("2100")) {
timestamp = Timestamp.valueOf(o.getDate() + " 21:00:00.0");
}