简体   繁体   中英

Updating Rows in Java using Jackcess

I am trying to update rows with a particular value using Jackcess in Java. I am using the below code and there is no changes done to the rows.

What I am missing here? I am feeling lost as there is no documentation for these methods.

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();

The following code works for me:

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

Note that column names are case-sensitive when working with Jackcess. The above code is updating a column named active , so

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

works, but

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

will not work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM