我正在尝试使用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值(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"
}
}
}
}
前面的JSON表示我的程序使用的数据的内部表示,它以这种方式构成:
{
"tables": {
"TableName": {
"rows": [
{
"items: {
"columnName1": "columnValue1",
...
"columnNameN": "columnValueN"
}
},
{
"items: {
"columnName1": "columnValue1",
...
"columnNameN": "columnValueN"
}
}
],
"columns": {
"columnName1": "columnDataType1",
...
"columnNameN": "columnDataTypeN"
}
}
}
}
如果不清楚请问我,
由于
答案 0 :(得分:3)
Jackcess通常希望使用强类型的Java对象。在这种情况下,Jackcess必须尝试通过在添加行时自动将String
转换为Double
来提供帮助。无论出于何种原因,Jackcess 愿意自动将String
转换为Boolean
* ,即使转换似乎很明显(对我们来说),因此对于包含字段的表
ID:自动编号,主键
DoubleField:数字(双)
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时尝试使用正确类型的对象,以避免很少的惊喜和#34;像这样,所以权利我们真的应该使用
String doubleFieldValue = "3.14159";
String yesnoFieldValue = "true";
tbl.addRow(Column.AUTO_NUMBER, Double.parseDouble(doubleFieldValue), Boolean.parseBoolean(yesnoFieldValue));
* 编辑:2015-03-21
将String
值自动转换为Boolean
已添加到今天发布的Jackcess 2.0.9中。
答案 1 :(得分:0)
在线:
List<String> values = new ArrayList<String>();
您明确指定希望值仅包含String对象, 如果你想插入布尔值,你需要一个更通用的对象声明。
List<Object> values = new ArrayList<Object>();
虽然你可能会失去一些必要的字符串功能。
答案 2 :(得分:0)
在 SharedPreferences 的情况下,由于在 SharedPreferences 中对布尔值和字符串使用相同的关键字,会出现此异常,这是我的 Details Answer.