Hi,
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:
publicvoiddumpDB()throwsIOException,Exception{// for each tablefor(StringtableName:this.DB.getTablesNames()){System.out.println(tableName);intnColumns=0;ModelDatabaseTabletable=this.DB.getTable(tableName);// create a tablebuilderTableBuilderDBTableBuilder=newTableBuilder(tableName);// get datatypes of columnsMap<String,DataType>columns=table.getColumns();// for each columnfor(StringcolumnName:columns.keySet()){System.out.println(columnName);// get its datatypeDataTypedt=columns.get(columnName);// create a column with correspondent datatype and max length and add it// to the table builderColumnBuildercb=newColumnBuilder(columnName).setType(dt).setMaxLength();DBTableBuilder.addColumn(cb);nColumns+=1;}// if table has columnsif(nColumns>0){// save it to the actual database: Exception rises hereTableDBTable=DBTableBuilder.toTable(this.DBConnection);// copy all table's rowsfor(ModelDatabaseRowrow:table.getRows()){List<String>values=newArrayList<String>();for(StringcolumnName:columns.keySet()){StringcolumnValue=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:
ColumnBuilder.setMaxLength() is probably not as smart as it could be. It's only really valid for variable length columns. To fix your code, use something like:
if(dt.isVariableLength()){
cb.setMaxLength();
}
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi,
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:
When I try to save the table to the actual database, I get the exception:
DataTypes were saved before using the same database I am writing (I am basically updating the database), using the code:
What am I doing wrong?
Thanks,
Last edit: Francesco Brundu 2015-01-29
ColumnBuilder.setMaxLength() is probably not as smart as it could be. It's only really valid for variable length columns. To fix your code, use something like:
Thank you!
Is there a particular advantage of using
dt.isVariableLength()instead ofdt.isTrueVariableLength()?Thanks
for this specific code block, either one would work.
Would you have any objections to someone submitting a ticket with a request to make it smarter? :)
Saves me from writing it myself!
Filed as https://sourceforge.net/p/jackcess/bugs/116/, will be in the 2.0.9 release.