【发布时间】:2017-03-31 21:04:32
【问题描述】:
我正在尝试删除保存在数据库中的时间列表,其中日期(end_time - 时间戳)大于我对特定员工的参考日期(employeeId - 整数)。 该查询在 pgAdmin III 上完美运行。但是我不能让它在 JDBC 中工作。 它有效,但它会删除该特定员工的所有内容,而不仅仅是我想要的日期。我看起来它忽略了 Where 子句中的日期。另外,我没有任何例外。 变量 nextDayTimestamp 的结果如下:'2017-03-31 12:00:00.0' 有人可以帮忙吗?
public void deleteBigListOfPeriodOfWorkFutureOnly(int employeeId, LocalDate referenceDate) {
Connection myConn = null;
PreparedStatement myStmt = null;
try {
myConn = dataSource.getConnection();
LocalDate nextDay = referenceDate.plusDays(1);
Timestamp nextDayTimestamp = Timestamp.valueOf(LocalDateTime.of(nextDay, LocalTime.NOON));
String sql = "delete from working_time where working_time.end_time > ? and employee_id = ?";
myStmt = myConn.prepareStatement(sql);
myStmt.setTimestamp(1, nextDayTimestamp);
myStmt.setInt(2, employeeId);
myStmt.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
CloseConnection.close(myConn, myStmt, null);
}
}
}
【问题讨论】:
-
您的数据库中的
end_time是什么,时间戳还是日期时间? -
我猜第一个条件总是正确的,就像@GuillaumeF.所说的那样,它可能与类型有关。
-
@GuillaumeF。 end_time 是没有时区的时间戳。
-
@JunbangHuang 我还为我的变量尝试了 Date 而不是 Timestamp。就我而言, where 中的第一个条件是日期条件,看起来它被忽略了。仅识别第二个条件“employee_id = ?”。我之前也尝试过交换它们,但也没有用。还有其他方法可以进行此查询吗?
-
@evzpav 你能试试把这个“working_time.end_time”改成“end_time”吗?
标签: java postgresql jdbc sql-delete