0

I am using apache Poi to pull data from an excel sheet that's uploaded to the server. The object is to pull the excel data, which has four columns (clientId, clientName, monthlyMinimum, setUpFee), and update an existing row's monthly minimum & set up fee, based off the clientId, in an existing database table named client. I have this code in a different section of my project to Insert a new client and it works, but am struggling to get it to update an existing one. Can anyone tell me what I am doing wrong with the clientId, where it is not updating these two fields?

    @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/";
    }


}

Update: I updated the code to include the wisdom from one of the comments, but it's still not quite working. I am getting stuck inside the if statement for count modulus batchsize. This is the Database error it is throwing: java.sql.BatchUpdateException: Incorrect syntax near '('.

2 Answers 2

1

clientId is not set:

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

Write the clientId as a parameter and set it after the cellIterator:

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

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

The order of the query parameters is also wrong:

[monthlyMinimum, setUpFee, clientId] are [1,2,3] respectively.

so monthlyMinimun and setUpFee should be:

statement.setFloat(1, monthlyMinimum);

statement.setFloat(2, setUpFee);

Sign up to request clarification or add additional context in comments.

2 Comments

this makes sense! I've added/updated my code, but now it is throwing an error at the end, where if (count % batchSize == 0) { statement.executeBatch(); } and I am not quite sure why
What database flavor are you using? The above syntax will work with Oracle. You can alternatively write the update statement as UPDATE client SET monthlyMinimum = ?, setUpFee=? WHERE ...
0

The answer above solved half of my issues. The other half of the issue was the update statement. Should be String sql = "UPDATE client SET monthlyMinimum = ? , oliSetUpFee= ? WHERE clientId = ?";

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.