简体   繁体   English

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

[英]java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean, using Table.addRow() with Jackcess

I am trying to write some values in a MS Access database using Jackcess.我正在尝试使用 Jackcess 在 MS Access 数据库中写入一些值。 My values are originally represented using String.我的值最初使用字符串表示。 The code I am using is the following:我使用的代码如下:

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

A basic example which does not work is the following (routine data is described using JSON).下面是一个不起作用的基本示例(使用 JSON 描述常规数据)。 In this case the routine stops when trying to insert BOOLEAN values ( HasM , HasZ ) but it is able to insert DOUBLE values - which are all given as parameters to Table.addRow() function as a String array.在这种情况下,例程在尝试插入 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"
      } 
    }
  }
}

The preceding JSON represents the internal representation of data used by my program and it is structured in this way:前面的 JSON 表示我的程序使用的数据的内部表示,它的结构如下:

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

In case it is not clear ask me,如果不清楚可以问我,

Thanks谢谢

Jackcess normally expects to work with strongly-typed Java objects. Jackcess 通常期望使用强类型 Java 对象。 In this case Jackcess must be trying to be helpful by automatically converting a String to a Double when adding the Row.在这种情况下,Jackcess 必须在添加行时通过自动将String转换为Double来提供帮助。 For whatever reason, Jackcess is not willing to automatically convert a String to a Boolean * , even if that conversion seems obvious (to us), so for a table with fields无论出于何种原因,Jackcess不愿意自动将转换StringBoolean *,即使转换似乎是显而易见的(给我们),所以与字段的表

ID: AutoNumber, Primary Key ID:自动编号,主键
DoubleField: Numeric(Double) DoubleField:数字(双)
YesnoField: Yes/No YesnoField:是/否

the following code will throw the exception you are seeing以下代码将抛出您看到的异常

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

However, this will work但是,这将起作用

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

As a general rule it is best to try and use objects of the correct type when working with Jackcess to avoid little "surprises" like this, so by rights we really should be using作为一般规则,最好在与 Jackcess 合作时尝试使用正确类型的对象,以避免像这样的小“惊喜”,所以根据权利,我们真的应该使用

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

* Edit: 2015-03-21 *编辑:2015-03-21

Automatic conversion of String values to Boolean was added to Jackcess 2.0.9, released today.String值自动转换为Boolean已添加到今天发布的 Jackcess 2.0.9 中。

On the line:在线上:

List<String> values = new ArrayList<String>();

you specifically specify you want values to only contain String objects, if you want to insert Booleans you need a more generic object declaration.您明确指定您希望值仅包含 String 对象,如果您想插入布尔值,您需要一个更通用的对象声明。

List<Object> values = new ArrayList<Object>();

though you'll probably lose some needed string functionality.虽然你可能会失去一些需要的字符串功能。

在 SharedPreferences 的情况下,由于在 SharedPreferences 中对布尔值和字符串使用相同的关键字,会出现此异常,这是我的详细信息答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 JRException:java.lang.ClassCastException:java.lang.String无法转换为java.lang.Boolean - JRException: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean Prefs错误-Android&#39;原因:java.lang.ClassCastException:java.lang.Boolean无法转换为java.lang.String&#39; - Prefs Error - Android ' Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String' java.lang.ClassCastException:无法在Struts的logic:equal标签中将java.lang.Boolean强制转换为java.lang.String - java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String in Struts' logic:equal tag java.lang.ClassCastException:无法将java.lang.Boolean强制转换为java.util.Map - java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.util.Map Mockito:java.lang.ClassCastException:java.lang.Boolean无法强制转换为DataListListener - Mockito : java.lang.ClassCastException: java.lang.Boolean cannot be cast to DataListListener Hibernate问题java.lang.Boolean不能转换为java.lang.String使用JDK 1.7 - Hibernate issue java.lang.Boolean cannot be cast to java.lang.String Using JDK 1.7 java.lang.ClassCastException:无法将java.lang.Boolean强制转换为org.apache.pig.data.Tuple - java.lang.ClassCastException: java.lang.Boolean cannot be cast to org.apache.pig.data.Tuple serverError:类java.lang.ClassCastException java.lang.Integer无法转换为java.lang.String - serverError: class java.lang.ClassCastException java.lang.Integer cannot be cast to java.lang.String Hiibernate:java.lang.ClassCastException:java.lang.String无法转换为java.lang.Integer - Hiibernate : java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer java.lang.RuntimeException:java.lang.ClassCastException:[B不能强制转换为java.lang.String - java.lang.RuntimeException: java.lang.ClassCastException: [B cannot be cast to java.lang.String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM