好的,所以我目前有一个有以下时间的数据库:
id 1 startTime 2013-09-09 15:05:10.0 endTime 2013-09-09 15:05:10.0
id 2 startTime 2013-09-09 15:09:54.0 endTime 2013-09-09 15:09:54.0
id 3 startTime 2013-09-09 15:20:46.0 endTime 2013-09-09 15:20:46.0
id 4 startTime 2013-09-09 15:21:06.0 endTime 2013-09-09 15:21:06.0
id 5 startTime 2013-09-09 15:21:34.0 endTime 2013-09-09 15:21:34.0
id 6 startTime 2013-09-09 15:22:34.0 endTime 2013-09-09 15:22:34.0
id 7 startTime 2013-09-09 15:23:06.0 endTime 2013-09-09 15:25:34.0
现在,当我按位于此处的时间方法运行搜索时:
@Override
public ArrayList<AppointmentAccess> searchByTime(Timestamp startTime,
Timestamp endTime) throws SQLException {
ArrayList<AppointmentAccess> appointmentList = new ArrayList<AppointmentAccess>();
String preparedQuery = "Select DISTINCT * From Appointments where startTime <= appointments.endTime AND endTime >= appointments.startTime";
// Connect to database
try (Connection connection = DriverManager.getConnection(url, user,
password);
// Run SQL
PreparedStatement ps = connection.prepareStatement(preparedQuery);
// Get SQL results
ResultSet query = ps.executeQuery();) {
while (query.next()) {
AppointmentAccess appointment = new AppointmentAccess();
appointment.setStartTime(query.getTimestamp("starttime"));
appointment.setEndTime(query.getTimestamp("endtime"));
appointment.setAlarmReminder(query
.getBoolean("alarmreminder"));
appointment.setAllDay(query.getBoolean("allday"));
appointment.setDetails(query.getString("details"));
appointment.setLocation(query.getString("location"));
appointment.setTitle(query.getString("title"));
appointmentList.add(appointment);
}
}
// Returns a List of all the contacts
return appointmentList;
}
我的测试方法“searchTooLate,searchTooEarly和searchTimeBetweenAppointments”仍然失败。 我发送这些方法的时间是:
startTime:“2013-09-09 16:05:09” 结束时间:“2013-09-09 16:22:35”
startTime:“2013-09-09 15:24:06.0” 结束时间:“2013-09-09 15:25:30.0”
startTime:“2013-08-09 14:05:09” 结束时间:“2013-08-09 16:22:35”
我做错了什么!?
答案 0 :(得分:0)
考虑代码的这一部分:String preparedQuery = "Select DISTINCT * From Appointments where startTime <= appointments.endTime AND endTime >= appointments.startTime";
您如何建议会改变?您每次都将startTime与“Appointments.editTime”进行比较(故意用双引号)。这会导致查询失败,因为Appointments.editTime不是数据库中的变量。
也许你打算..." + Appointments.startTime.ToString() + "...
这仍然是错误的方法,但这将使你的代码正常运行。考虑一个PROC和参数。