I’m trying to select all rows that fall between two column values. The values are UNIX Timestamps but the query isn’t returning any rows at all. Why would that be?
Here’s my code:
public static ArrayList<Appointment> getAppointments(long start, long end) {
ArrayList<Appointment> appointments = new ArrayList<>();
try {
Statement stat = db.createStatement();
System.out.println("\n" + start + " -> " + end);
// debugging only
try (ResultSet rs = stat.executeQuery("SELECT * FROM appointments;")) {
while (rs.next()) {
long time = rs.getLong("date");
if (time >= start && time <= end) {
System.out.println("Should be adding to list");
}
}
} //
try (ResultSet rs = stat.executeQuery("SELECT * FROM appointments WHERE date BETWEEN " + start + " AND " + end + ";")) {
while (rs.next()) {
System.out.println("ADDING");
appointments.add(new Appointment(rs.getLong("date"), rs.getInt("duration")));
}
}
} catch (SQLException ex) {
}
System.out.println(appointments);
return appointments;
}
Where the table has three rows with these values:
1347390000000 15
1347461100000 30
1347469200000 45
And the output is:
date = 1347390000000 duration = 15
date = 1347461100000 duration = 30
date = 1347469200000 duration = 45
1350141222283 -> 1350746022283
[]
1349536422283 -> 1350141222283
[]
1348931622283 -> 1349536422283
[]
1348330422283 -> 1348931622283
[]
1347725622283 -> 1348330422283
[]
1347120822283 -> 1347725622283
Should be adding to list
Should be adding to list
Should be adding to list
[]
1346516022283 -> 1347120822283
[]
You have to declare the data types of the columns.