【问题标题】:Updating Rows in Java using Jackcess使用 Jackcess 在 Java 中更新行
【发布时间】:2014-06-10 06:15:03
【问题描述】:

我正在尝试使用 Java 中的 Jackcess 更新具有特定值的行。我正在使用下面的代码,并且没有对行进行任何更改。

我在这里缺少什么?由于没有这些方法的文档,我感到很失落。

Database db = DatabaseBuilder.open(new File("Db.mdb"));
Table table = db.getTable("Table1");

Cursor cursor = CursorBuilder.createCursor(table);

Map<String, Object> map = new HashMap<String, Object>();

map.put("Active", true); // Value to be updated

for (Row row : cursor.newIterable().addMatchPattern("testnum", testNum)) { 
     cursor.updateCurrentRow(table.asUpdateRow(map));
}

db.flush();
db.close();

【问题讨论】:

  • 你考虑过使用UCanAccess吗?有关更多信息,请参阅问题here
  • 是的。几个月前,你为我的一个问题提出了这个建议。但遗憾的是,由于某些政策,UcanAccess 无法在我的环境中使用。

标签: java jackcess


【解决方案1】:

以下代码适用于我:

String dbFile = "C:/Users/Public/test/DB.mdb";
try (Database db = DatabaseBuilder.open(new File(dbFile))) {
    Table table = db.getTable("Table1");
    Cursor cursor = CursorBuilder.createCursor(table);
    int testNum = 1;
    for (Row row : cursor.newIterable().addMatchPattern("testnum", testNum)) {
        row.put("active", true);
        table.updateRow(row);
    }
} catch (Exception e) {
    e.printStackTrace(System.out);
}

请注意,使用 Jackcess 时,列名区分大小写。上面的代码正在更新一个名为active的列,所以

row.put("active", true);
table.updateRow(row);

有效,但是

row.put("Active", true);
table.updateRow(row);

不会工作。

【讨论】:

  • 这行得通。我觉得所有这些重要的操作都应该记录在官方页面上。非常感谢。
  • 感谢您的提示。我刚刚注意到了。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2014-11-07
  • 2017-11-29
  • 1970-01-01
相关资源
最近更新 更多