java.lang.ClassCastException:java.lang.String 不能转换为 java.lang.Boolean,使用 Table.addRow() 和 Jackcess
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28237009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Boolean, using Table.addRow() with Hymancess
提问by gc5
I am trying to write some values in a MS Access database using Hymancess. My values are originally represented using String. The code I am using is the following:
我正在尝试使用 Hymancess 在 MS Access 数据库中写入一些值。我的值最初使用字符串表示。我使用的代码如下:
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 Hymancess 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). 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.
下面是一个不起作用的基本示例(使用 JSON 描述常规数据)。在这种情况下,例程在尝试插入 BOOLEAN 值 ( HasM
, HasZ
)时停止,但它能够插入 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
谢谢
采纳答案by Gord Thompson
Hymancess normally expects to work with strongly-typed Java objects. In this case Hymancess must be trying to be helpful by automatically converting a String
to a Double
when adding the Row. For whatever reason, Hymancess is notwilling to automatically convert a String
to a Boolean
*, even if that conversion seems obvious (to us), so for a table with fields
Hymancess 通常期望使用强类型 Java 对象。在这种情况下,Hymancess 必须尝试通过在添加行时自动将 a 转换为 aString
来提供帮助Double
。无论出于何种原因,Hymancess是不是愿意自动转换String
为Boolean
*,即使转换似乎是显而易见的(给我们),所以与字段的表
ID: AutoNumber, Primary Key
DoubleField: Numeric(Double)
YesnoField: Yes/No
ID:自动编号,主键
DoubleField:数字(双)
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 Hymancess to avoid little "surprises" like this, so by rights we really should be using
作为一般规则,最好在与 Hymancess 合作时尝试使用正确类型的对象,以避免像这样的小“惊喜”,所以根据权利,我们真的应该使用
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 Hymancess 2.0.9, released today.
今天发布的 Hymancess 2.0.9 添加了String
值的自动转换Boolean
。
回答by Bryan Devaney
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.
虽然你可能会失去一些需要的字符串功能。