我是编码新手,但有一个问题:我使用Apache Poi读取只有一列和大约20行的Excel文件。我想阅读该信息并将其插入到我的Access数据库中,但是我不知道该怎么做。
这是我的第一种方法:
Workbook readWorkbook = WorkbookFactory.create(new FileInputStream(FileChooser.FileChoose()));
Sheet sheet = readWorkbook.getSheetAt(4);
DataFormatter dataFormatter = new DataFormatter();
String cellValue = null;
for (int i = 16; i < 20; i++) { //Statt 20 soll hier 39
Row row = sheet.getRow(i);
if(row == null) {
continue;
}
for (int j = 2; j < 4; j++) {
Cell cell = row.getCell(j);
if(cell == null) {
continue;
}
cellValue = dataFormatter.formatCellValue(cell);
System.out.print(cellValue + "\n");
}
}
return cellValue;
这是我的插入代码:
try {
Connection conn=DriverManager.getConnection("jdbc:ucanaccess://"+FileChooser.FileChoose());
Statement stment = conn.createStatement();
String query = "INSERT INTO Maschine (com_id, typ, a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p, q, r, s, t, u, v) VALUES ('"+DatenBankInsert.ForEachLoop()+"')";
stment.executeUpdate(query);
System.out.println("successfull");
}
catch (Exception e) {
System.out.println("Not connected"+e);
}
return null;