首页 > 解决方案 > 使用 Java,从 Excel 表更新现有 SQL 表行

问题描述

我正在使用 apache Poi 从上传到服务器的 Excel 表中提取数据。目标是提取具有四列(clientId、clientName、monthlyMinimum、setUpFee)的 excel 数据,并根据 clientId 在名为 client. 的现有数据库表中更新现有行的每月最低和设置费用。我在项目的不同部分中有此代码来插入新客户端并且它可以工作,但我正在努力让它更新现有的客户端。谁能告诉我我在 clientId 上做错了什么,它没有更新这两个字段?

    @RequestMapping(value="/save/acctDocument")
    public String addAcctDoc(Model model, @ModelAttribute(value="newDocument") Document newDocument, @RequestParam("document") MultipartFile file) throws IOException{
//        Document doc=documentRepository.save(newDocument);
        newDocument.setStorage(storageService.storer(file));  //uploads the doc
        //find doc, read doc, save info to db client table
        int batchSize = 20;

        Connection connection = null;

        try {
            long start = System.currentTimeMillis();

            FileInputStream inputStream = new FileInputStream(appDir+"\\AcctUploadFile.xlsx");

            Workbook workbook = new XSSFWorkbook(inputStream);

            Sheet firstSheet = workbook.getSheetAt(0);
            Iterator<Row> rowIterator = firstSheet.iterator();

            connection = DriverManager.getConnection(jbdcUrl, dbusername, dbpassword);
            connection.setAutoCommit(false);

            String sql = "UPDATE client SET (monthlyMinimum, oliSetUpFee) VALUES (?, ?) WHERE  clientId = (?)";
            PreparedStatement statement = connection.prepareStatement(sql);

            int count = 0;

            rowIterator.next(); // skip the header row

            while (rowIterator.hasNext()) {
                Row nextRow = rowIterator.next();
                Iterator<Cell> cellIterator = nextRow.cellIterator();

                while (cellIterator.hasNext()) {
                    Cell nextCell = cellIterator.next();

                    int columnIndex = nextCell.getColumnIndex();

                    switch (columnIndex) {
                                                case 1:
                        long clientId = (long) nextCell.getNumericCellValue();
                        statement.setLong(3, clientId);
                        break;
                    case 2:
                        float monthlyMinimum = (float) nextCell.getNumericCellValue();
                        statement.setFloat(1, monthlyMinimum);
                        break;
                    case 3:
                       float setUpFee= (float) nextCell.getNumericCellValue();
                        statement.setFloat(2, setUpFee);
                        break;
                    }

                }

                statement.addBatch();

                if (count % batchSize == 0) {
                    statement.executeBatch();
                }

            }

            workbook.close();

            // execute the remaining queries
            statement.executeBatch();

            connection.commit();
            connection.close();
            File acctUploadFile = new File(appDir+"\\AcctUploadFile.xlsx");
            acctUploadFile.delete();

            long end = System.currentTimeMillis();
            System.out.printf("Import done in %d ms\n", (end - start));

        } catch (IOException ex1) {
            System.out.println("Error reading file");
            ex1.printStackTrace();
        } catch (SQLException ex2) {
            System.out.println("Database error");
            ex2.printStackTrace();
        }
        return  "redirect:/reports/";
    }


}

更新:我更新了代码以包含其中一条评论中的智慧,但它仍然不能正常工作。我陷入了计算模数批量大小的 if 语句中。这是它抛出的数据库错误:java.sql.BatchUpdateException:'('附近的语法不正确。

标签: javasqlapache-poi

解决方案


未设置clientId :

String sql = "UPDATE client SET (monthlyMinimum, setUpFee) VALUES (?, ?) WHERE clientId = clientId";

将clientId写为参数,在cellIterator后面设置:

String sql = "UPDATE client SET (monthlyMinimum, setUpFee) VALUES (?, ?) WHERE clientId = ?";

statement.setLong(3, clientId) 或 setInteger(),

查询参数的顺序也是错误的:

[monthlyMinimum, setUpFee, clientId] 分别为 [1,2,3]。

所以monthlyMinimun 和setUpFee 应该是:

statement.setFloat(1, monthlyMinimum);

statement.setFloat(2, setUpFee);


推荐阅读