orion_IX orion_IX - 1 year ago 85
Java Question

Column data substring matching with Jackcess

In my Access db table, I have a

cmt_data
column which contains a string. For example:

Check in the packet TM(12,9) MRSS0319 'Monitoring List Report'.


I also have a
List<String>
with items such as
MRSS0319
,
TRPP3006
, etc. What I'm looking to do is perform a substring match between my
List<String>
and the table column, but I can't quite figure out how as the examples provided with Jackcess is rather simple. An example I found here shows:

Column col = table.getColumn("town");
cursor.beforeFirst();
while(cursor.moveToNextRow()) {
if(cursor.currentRowMatches(columnPattern, valuePattern)) {
// handle matching row here
}
}


Where the method
cursor.currentRowMatches(columnPattern, valuePattern)
looks like it could be useful. However according to the documentation it seems that the method only performs string equality matching, so now I'm sort of at a dead end.

Appreciate your help.

Answer Source

Certainly you could create a little method to check the cmt_data value for a match:

public static void main(String[] args) {
    String dbPath = "C:/Users/Public/JackcessTest.accdb";
    try (Database db = DatabaseBuilder.open(new File(dbPath))) {
        Table table = db.getTable("cmt_table");
        Cursor cursor = table.getDefaultCursor();
        cursor.beforeFirst();
        while (cursor.moveToNextRow()) {
            Row row = cursor.getCurrentRow();
            if (stringContainsSpecialValue(row.getString("cmt_data"))) {
                // handle matching row here
                System.out.println(row);
            }
        }
    } catch (Exception e) {
        e.printStackTrace(System.err);
    }
}

private static boolean stringContainsSpecialValue(String str) {
    boolean rtn = false;
    List<String> specialValues = Arrays.asList("MRSS0319", "TRPP3006");
    for (String val : specialValues) {
        if (str.contains(val)) {
            rtn = true;
            break;
        }
    }
    return rtn;
}

You could probably also create a custom ColumnMatcher for your cursor, but that might be overkill.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download