我正在尝试使用 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();
最佳答案
以下代码适用于我:
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);
将不起作用。
关于java - 使用 Jackcess 在 Java 中更新行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24134101/