gpt4 book ai didi

java.lang.ClassCastException : java. lang.String 无法转换为 java.lang.Boolean,使用 Table.addRow() 和 Jackcess

转载 作者:搜寻专家 更新时间:2023-11-01 03:04:15 24 4
gpt4 key购买 nike

我正在尝试使用 Jackcess 在 MS Access 数据库中写入一些值。我的值最初是使用 String 表示的。我使用的代码如下:

int nColumns = 0;

// get table from internal representation
ModelDatabaseTable table = this.DB.getTable(tableName);

// create new table
TableBuilder DBTableBuilder = new TableBuilder(tableName);

// get table's columns and their Jackcess datatype
Map<String, DataType> columns = table.getColumns();

// for each column, insert it in the actual database
for (String columnName : columns.keySet()) {
DataType dt = columns.get(columnName);
ColumnBuilder cb = new ColumnBuilder(columnName).setType(dt);
if (dt.isVariableLength()) {
cb.setMaxLength();
}
DBTableBuilder.addColumn(cb);
nColumns += 1;
}

// if columns were inserted
if (nColumns > 0) {
// write table to actual database
Table DBTable = DBTableBuilder.toTable(this.DBConnection);

// for each row
for (ModelDatabaseRow row : table.getRows()) {

// get list of values (represented using String)
List<String> values = new ArrayList<String>();

// for each column get its value and insert it in values
for (String columnName : columns.keySet()) {
String columnValue = row.getColumn(columnName);
values.add(columnValue);
}

// print current row
System.out.println(values.toString());

// insert row in database table. Exception rises here:
// java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean
DBTable.addRow(values.toArray());
}
}

下面是一个不起作用的基本示例(常规数据使用 JSON 描述)。在这种情况下,例程在尝试插入 BOOLEAN 值(HasMHasZ)时停止,但它能够插入 DOUBLE 值 - 这些值都作为参数提供给 Table.addRow() 函数作为一个字符串数组。

{
"tables": {
"Table1": {
"rows": [
{
"items": {
"ExtentBottom": "45.050715999999994",
"ExtentLeft": "7.644834000000003",
"ExtentRight": "7.670400999999998",
"ExtentTop": "45.07392899999999",
"FieldName": "Shape",
"HasM": "false",
"HasZ": "false",
"IdxGridSize": "3.7252903001966386E-7",
"IdxOriginX": "0.0",
"IdxOriginY": "0.0",
"MHigh": "NaN",
"MLow": "NaN",
"SRID": "1",
"ShapeType": "4",
"TableName": "GDB_Items",
"ZHigh": "NaN",
"ZLow": "NaN"
}
},
{
"items": {
"ExtentBottom": "4989476.8181",
"ExtentLeft": "393329.1171000004",
"ExtentRight": "395300.25320000015",
"ExtentTop": "4992023.569399999",
"FieldName": "Shape",
"HasM": "false",
"HasZ": "false",
"IdxGridSize": "0.009311329524584121",
"IdxOriginX": "0.0",
"IdxOriginY": "0.0",
"MHigh": "NaN",
"MLow": "NaN",
"SRID": "2",
"ShapeType": "4",
"TableName": "Building_DIMMER_01",
"ZHigh": "NaN",
"ZLow": "NaN"
}
}
],
"columns": {
"ExtentBottom": "DOUBLE",
"ExtentLeft": "DOUBLE",
"ExtentRight": "DOUBLE",
"ExtentTop": "DOUBLE",
"FieldName": "TEXT",
"HasM": "BOOLEAN",
"HasZ": "BOOLEAN",
"IdxGridSize": "DOUBLE",
"IdxOriginX": "DOUBLE",
"IdxOriginY": "DOUBLE",
"MHigh": "DOUBLE",
"MLow": "DOUBLE",
"SRID": "LONG",
"ShapeType": "LONG",
"TableName": "TEXT",
"ZHigh": "DOUBLE",
"ZLow": "DOUBLE"
}
}
}
}

前面的 JSON 表示我的程序使用的数据的内部表示,它的结构是这样的:

{
"tables": {
"TableName": {
"rows": [
{
"items: {
"columnName1": "columnValue1",
...
"columnNameN": "columnValueN"
}
},
{
"items: {
"columnName1": "columnValue1",
...
"columnNameN": "columnValueN"
}
}
],
"columns": {
"columnName1": "columnDataType1",
...
"columnNameN": "columnDataTypeN"
}
}
}
}

不懂的可以问我,

谢谢

最佳答案

Jackcess 通常希望使用强类型的 Java 对象。在这种情况下,Jackcess 必须尝试通过在添加行时自动将 String 转换为 Double 来提供帮助。无论出于何种原因,Jackcess 不愿意自动将 String 转换为 Boolean*/sup>,即使这种转换看起来很明显(对我们来说),所以对于带有字段的表

ID:自动编号,主键
DoubleField:数字( double )
YesnoField:是/否

下面的代码会抛出你看到的异常

String doubleFieldValue = "3.14159";
String yesnoFieldValue = "true";
tbl.addRow(Column.AUTO_NUMBER, doubleFieldValue, yesnoFieldValue);

但是,这会起作用

String doubleFieldValue = "3.14159";
String yesnoFieldValue = "true";
tbl.addRow(Column.AUTO_NUMBER, doubleFieldValue, Boolean.parseBoolean(yesnoFieldValue));

作为一般规则,最好在使用 Jackcess 时尝试使用正确类型的对象以避免像这样的小“意外”,所以按权利我们确实应该使用

String doubleFieldValue = "3.14159";
String yesnoFieldValue = "true";
tbl.addRow(Column.AUTO_NUMBER, Double.parseDouble(doubleFieldValue), Boolean.parseBoolean(yesnoFieldValue));

* 编辑:2015-03-21

今天发布的 Jackcess 2.0.9 添加了将 String 值自动转换为 Boolean 的功能。

关于java.lang.ClassCastException : java. lang.String 无法转换为 java.lang.Boolean,使用 Table.addRow() 和 Jackcess,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28237009/

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