2017-02-07 15 views
1

在我的Access數據庫表中,我有一個包含字符串的cmt_data列。例如:與Jackcess匹配的列數據子字符串

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

我也有項目,如MRSS0319TRPP3006等一List<String>什麼,我希望做的是我List<String>和表列之間進行串匹配,但我可以因爲Jackcess提供的例子相當簡單,所以不太清楚。我發現here一個例子說明:

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

凡方法cursor.currentRowMatches(columnPattern, valuePattern)看起來可能是有用的。然而,根據文檔,該方法似乎只執行字符串平等匹配,所以現在我有點死衚衕了。

感謝您的幫助。

+0

[Jackcess API Docs](http://jackcess.sourceforge.net/apidocs/) –

回答

2

當然,你可以創建一個小方法來檢查cmt_data值匹配:

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; 
} 

你也許還可以創建自定義ColumnMatcher爲你的光標,但可能是矯枉過正。

+0

很好的回答,但我正在尋找如果發現一個子字符串寫回更改到Access。在那種情況下,我是否必須執行'cursor.updateCurrentRow(row)'?我還注意到,示例文檔對於讓API失效的Jackcess項目有點缺乏。也許有關SO的文檔可以在'jackcess'標籤下打開?鏈接[here](http://stackoverflow.com/documentation/jackcess) –

+0

是的,你可以使用'cursor.updateCurrentRow'或'table.updateRow',或者'cursor.setCurrentRowValue'來更新一列。至於文檔方面,我會讓Jackcess團隊決定是否要在這裏維護文檔(而不是或者除了[cookbook](http://jackcess.sourceforge.net/cookbook.html) ,[Javadoc](http://jackcess.sourceforge.net/apidocs/)以及源代碼中的單元測試)。 –

+1

我正在使用'cursor.setCurrentRowValue(table.getColumn(「cmt_data」),newString);'這似乎按照需求工作。當然,我遇到過,只是如果有更多的使用場景示例,它會更好。等等,非常感謝您的幫助。 –