Jackcess相当于带有>的SQL WHERE子句(“严格大于”)条件

时间:2014-09-17 10:00:42

标签: java ms-access select where jackcess

我希望搜索所有行,其中Date列的值为> '2014年3月1日'。

怎么办呢?如果没有Date value ='2014 / 03/01'的行,如何在没有完整扫描表的情况下定位光标?

Table table = db.getTable("Facture");
IndexCursor cursor = CursorBuilder.createCursor(table.getIndex("DateIndex"));

Date search_date = Date('2014/03/01');
for(Row row : cursor.newEntryIterable(search_date)) { ... }

1 个答案:

答案 0 :(得分:1)

您已经在创建IndexCursor,因此无需进行表格扫描。只需使用IndexCursor#findClosestRowByEntry进行>=搜索,然后跳过完全匹配(如果有),就像这样:

Table table = db.getTable("Members");
String dateColumnName = "DateJoined";
Column dateColumn = table.getColumn(dateColumnName);
IndexCursor cursor = CursorBuilder.createCursor(table.getIndex(dateColumnName));

String searchDateAsString = "2014/03/01";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
Date search_date = sdf.parse(searchDateAsString);
cursor.findClosestRowByEntry(search_date);
if (cursor.isAfterLast()) {
    System.out.println(String.format("There are no rows with %s >= %s", dateColumnName, searchDateAsString));
}
else {
    // we want strictly greater than, so skip over the rows that are equal
    while (search_date.equals(cursor.getCurrentRowValue(dateColumn))) {
        if (!cursor.moveToNextRow()) break;
    }
    if (cursor.isAfterLast()) {
        System.out.println(String.format("There are no rows with %s > %s", dateColumnName, searchDateAsString));
    }
}
// now iterate over the remaining rows
while (!cursor.isAfterLast()) {
    System.out.println(sdf.format(cursor.getCurrentRowValue(dateColumn)));
    cursor.moveToNextRow();
}