1

I need help in a routine I've written to dump the content of a class (which represent a database table) to a new database table in MS Access. My code is the following:

public void dumpDB() throws IOException, Exception {
    // for each table
    for (String tableName : this.DB.getTablesNames()) {
        System.out.println(tableName);
        int nColumns = 0;
        ModelDatabaseTable table = this.DB.getTable(tableName);

        // create a tablebuilder
        TableBuilder DBTableBuilder = new TableBuilder(tableName);

        // get datatypes of columns
        Map<String, DataType> columns = table.getColumns();

        // for each column
        for (String columnName : columns.keySet()) {
            System.out.println(columnName);

            // get its datatype
            DataType dt = columns.get(columnName);

            // create a column with correspondent datatype and max length and add it
            // to the table builder
            ColumnBuilder cb = new ColumnBuilder(columnName).setType(dt).setMaxLength();
            DBTableBuilder.addColumn(cb);
            nColumns += 1;
        }
        // if table has columns
        if (nColumns > 0) {

            // save it to the actual database: Exception rises here
            Table DBTable = DBTableBuilder.toTable(this.DBConnection);


            // copy all table's rows
            for (ModelDatabaseRow row : table.getRows()) {
                List<String> values = new ArrayList<String>();
                for (String columnName : columns.keySet()) {
                    String columnValue = row.getColumn(columnName);
                    values.add(columnValue);
                }
                DBTable.addRow(values.toArray());
            }
        }
    }
}

When I try to save the table to the actual database, I get the exception:

java.lang.IllegalArgumentException: invalid fixed length size
at com.healthmarketscience.jackcess.ColumnBuilder.validate(ColumnBuilder.java:361)
at com.healthmarketscience.jackcess.impl.TableCreator.validate(TableCreator.java:207)
at com.healthmarketscience.jackcess.impl.TableCreator.createTable(TableCreator.java:130)
at com.healthmarketscience.jackcess.impl.DatabaseImpl.createTable(DatabaseImpl.java:954)
at com.healthmarketscience.jackcess.TableBuilder.toTable(TableBuilder.java:223)
at modelDatabase.AccessModelDatabaseBuilder.dumpDB(AccessModelDatabaseBuilder.java:153)
at modelDatabase.AccessModelDatabaseBuilder.main(AccessModelDatabaseBuilder.java:37)

DataTypes were saved before using the same database I am writing (I am basically updating the database), using the code:

for (Column column : DBTable.getColumns()) {
    table.addColumn(column.getName(), column.getType(), "");
}

What am I doing wrong?

4

1 回答 1

1

Jackcess 论坛主题中,解决方案是包装对setMaxLength()方法的调用:

if(dt.isVariableLength()) {
    cb.setMaxLength();
}
于 2015-01-29T17:11:24.870 回答