Search code examples
javams-accessjackcess

How can I create and insert data into a table with a special name?


I'm trying to use the jackcess library to create a database and import data into it. The problem I have found is that if I create a table named "type", which is a reserved word for jackcess, then I can not import data into this table using the ImportBuilder. A new table is generated with the prefix "x" (the new table will be named "xtype").

I also have tried to create the table with another name, import data and the rename to reserved name. But I wasn't able to find any method to rename tables.

The table has to be named "type".

What should I do?


Solution

  • Jackcess does appear reluctant to import a text file directly into a new table named type, but the following code seems to do the trick:

    // open existing database
    Database db = DatabaseBuilder.open(new File(
            "C:/Users/Gord/Desktop/foo.accdb"));
    
    String tempTableName = "TemporaryNameForTable";
    
    // import CSV file into new table with temporary name
    ImportUtil.Builder ib = new ImportUtil.Builder(db);
    ib.setTableName(tempTableName);
    ib.importFile(new File("C:/Users/Gord/Desktop/foo.csv"));
    
    // rename the new table
    Table mso = db.getSystemTable("MSysObjects");
    Row r = CursorBuilder.findRow(mso,
            Collections.singletonMap("Name", tempTableName));
    r.put("Name", "type");  // new name is "type"
    mso.updateRow(r);
    db.close();