4

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();
Purus
  • 5,227
  • 6
  • 40
  • 83
  • 1
    Have you considered using [UCanAccess](http://ucanaccess.sourceforge.net/site.html)? For more information, see the question [here](http://stackoverflow.com/q/21955256/2144390). – Gord Thompson Jun 10 '14 at 10:37
  • Yes. Few months back, you suggested this for one of my questions. But unfortunately, UcanAccess can't be used in my environment due to some policies. – Purus Jun 10 '14 at 12:48

1 Answers1

1

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.

Gord Thompson
  • 98,607
  • 26
  • 164
  • 342