Excelファイルからデータを読み取ってテーブルに保存するプログラムを作成しようとしています。複数のファイルがあり、コードはそれらのほとんどで正常に機能します。しかし、日付列を含むファイルを試すと、いくつかの問題が発生します。
まず、コンソールにデータを表示するために、さまざまなタイプのデータに対して以下のコードを作成しました。
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"dd/MM/yyyy");
System.out.println(cell.getDateCellValue());
} else {
Double value = cell.getNumericCellValue();
Long longValue = value.longValue();
System.out.print(cell.getNumericCellValue());
}
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
System.out.print(cell.getRichStringCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
System.out.print(cell.getBooleanCellValue());
} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
System.out.print("null");
}
さらに、データを解析するときは、さまざまな種類のデータに対して以下のコードを使用しています。
if (cell != null) {
if (cell.getCellType() == Cell.CELL_TYPE_NUMERIC) {
if (DateUtil.isCellDateFormatted(cell)) {
tableFields.put(String.valueOf(cell
.getDateCellValue()), cell.getCellType());
} else {
tableFields
.put(String.valueOf(cell
.getNumericCellValue()), cell
.getCellType());
}
} else if (cell.getCellType() == Cell.CELL_TYPE_STRING) {
tableFields.put(cell.getStringCellValue(), cell
.getCellType());
} else if (cell.getCellType() == Cell.CELL_TYPE_BOOLEAN) {
tableFields.put(String.valueOf(cell
.getBooleanCellValue()), cell.getCellType());
} else if (cell.getCellType() == Cell.CELL_TYPE_BLANK) {
tableFields.put(cell.getStringCellValue(), cell
.getCellType());
}
テーブルを作成するとき:
switch (fieldType) {
case Cell.CELL_TYPE_NUMERIC:
str = fieldName + " dateFormat";
str = fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_STRING:
str = fieldName + " VARCHAR(255)";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = fieldName + " INTEGER";
break;
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
break;
}
最後に、テーブルに値を入力するときは、以下のコードを使用しています。
switch (fieldType) {
case Cell.CELL_TYPE_NUMERIC:
str = fieldValue;
break;
case Cell.CELL_TYPE_STRING:
str = "\'" + fieldValue + "\'";
break;
case Cell.CELL_TYPE_BOOLEAN:
str = fieldValue;
break;
case Cell.CELL_TYPE_BLANK:
str = "null";
break;
default:
str = "";
break;
}
しかし、プログラムを実行していると、次の例外が発生します。
SQLException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'Apr 21 00:00:00 EEST 1987)' at line 1
SQLState: 42000
VendorError: 1064
なぜ私がそれを得るのを誰か助けてもらえますか? 日付の形式: 26/4/1980