为什么我得到一个空的ResultSet:
int numberOfEvents = 5;
long starttime = 1511655677248;
long endtime = 1511684405497;
String query = "SELECT NAME FROM EVENTS WHERE TIME >= ? AND TIME <= ? GROUP BY NAME HAVING COUNT(*) > ? ;";
try (Connection connection = DBManager.getConnection(); PreparedStatement statement = connection.prepareStatement(query);) {
statement.setLong(1, starttime);
statement.setLong(2, endtime);
statement.setInt(3, numberOfEvents);
List<String> ids = new ArrayList<>();
ResultSet rs = statement.executeQuery();
while (rs.next()) {
ids.add(rs.getString("NAME"));
}
return ids;
} catch (SQLException e) {
throw new MessageException(e);
}
但是有了这个,我得到了预期的结果:
String query = "SELECT NAME FROM EVENTS WHERE TIME >= 1511655677248 AND TIME <= 1511684405497 GROUP BY NAME HAVING COUNT(*) > 5 ;";
try (Connection connection = DBManager.getConnection(); PreparedStatement statement = connection.prepareStatement(query);) {
List<String> ids = new ArrayList<>();
ResultSet rs = statement.executeQuery();
while (rs.next()) {
ids.add(rs.getString("NAME"));
}
return ids;
} catch (SQLException e) {
throw new MessageException(e);
}
这也给了我同样的预期结果:
long starttime = 1511655677248;
long endtime = 1511684405497;
int numberOfEvents = 5;
String query = "SELECT NAME FROM EVENTS WHERE TIME >= ? AND TIME <= ? GROUP BY NAME HAVING COUNT(*) > " + Integer.toString(numberOfEvents) + " ;";
try (Connection connection = DBManager.getConnection(); PreparedStatement statement = connection.prepareStatement(query);) {
statement.setLong(1, starttime);
statement.setLong(2, endtime);
List<String> ids = new ArrayList<>();
ResultSet rs = statement.executeQuery();
while (rs.next()) {
ids.add(rs.getString("NAME"));
}
return ids;
} catch (SQLException e) {
throw new MessageException(e);
}
所以我认为最后一个参数有问题。在HAVING子句中使用参数是否有特殊要求或限制?我使用的是sqldroid 1.0.3。