gpt4 book ai didi

java - 使用 Jackcess 库接收 java.lang.IllegalArgumentException : invalid fixed length size,

转载 作者:太空宇宙 更新时间:2023-11-04 06:14:53 26 4
gpt4 key购买 nike

我编写的例程需要帮助,以便将类的内容(代表数据库表)转储到 MS Access 中的新数据库表中。我的代码如下:

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

当我尝试将表保存到实际数据库时,出现异常:

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)

数据类型是在使用我正在编写的相同数据库之前保存的(我基本上是在更新数据库),使用代码:

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

我做错了什么?

最佳答案

Jackcess forum thread 中,解决方案是将调用包装到 setMaxLength() 方法:

if(dt.isVariableLength()) {
cb.setMaxLength();
}

关于java - 使用 Jackcess 库接收 java.lang.IllegalArgumentException : invalid fixed length size,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28217738/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com