【发布时间】:2015-03-29 23:52:34
【问题描述】:
我正在尝试使用 Jackcess 在 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 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"
}
}
}
}
不清楚的可以问我
谢谢
【问题讨论】:
-
如果有帮助的话,JSON 布尔值是真假(不带引号)...
-
这个错误对我来说似乎很清楚。在尝试将
String中的Boolean列插入您的Access 表之前,请先对其进行解析。Exception有什么不清楚的地方?您提到Double按您的预期工作,但这必须是您的数据库架构的定义方式(注意,您没有发布您的架构)。 -
@Johann-ChristophJacob 谢谢,但在这种情况下,所有数据类型都表示为字符串(我只使用 JSON 来可视化它们).. @ElliottFrisch 我不清楚的事实是数字@ 987654331@ 和
DOUBLE会自动从它们的字符串表示中解析,BOOLEAN不会。是的,我没有发布架构,因为我现在无法创建它。但是,该字段的数据类型显示在第二个代码部分的末尾。如果不清楚,请告诉我..
标签: java string ms-access boolean jackcess