Mohamed Moustapha
Mohamed Moustapha

Reputation: 9

Loop on Part and String get duplicated JSP/JEE

I have an input form which contains the ability to add multiple input type text and type file, and I'm trying to get all the values and insert them into my database. Can you guys tell me what is wrong with my codes duplicating all the values in the database?

String [] inst=request.getParameterValues("inst");

List<Part> fileParts = request.getParts().stream()
    .filter(part -> "picture".equals(part.getName()))
    .collect(Collectors.toList()); // Retrieves 

<input type="file" name="file" multiple="true">

for (int i=0; i< inst.length; i++ ) {
    /*   Part filePart2=request.getPart("picture");
         InputStream photo2 = null;
         photo2=filePart2.getInputStream();*/

    try {
        // connects to the database
        //Get all the parts from request and write it to the file on server
        for (Part filePartt : fileParts) {
            //   String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
            InputStream fileContent = filePartt.getInputStream();

            // ... (do your job here)
            Class.forName("com.mysql.jdbc.Driver");
            java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");
            PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
            pr.setString(1, inst[i]);
            System.out.println("numero "+i);

            if (fileContent != null) {
                // fetches input stream of the upload file for the blob column
                pr.setBlob(2, fileContent);
            }
            pr.setInt(3, idsub);
            pr.executeUpdate();

        }
    } catch(Exception e){}
}

Result

Upvotes: 1

Views: 88

Answers (2)

Mohamed Moustapha
Mohamed Moustapha

Reputation: 9

i manage to fix it `String [] inst=request.getParameterValues("inst");

      List<Part> fileParts = request.getParts().stream().filter(part -> "picture".equals(part.getName())).collect(Collectors.toList()); // Retrieves <input type="file" name="file" multiple="true">

      Class.forName("com.mysql.jdbc.Driver");
      java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");

      int i=0;  

            try {
                // connects to the database



            //Get all the parts from request and write it to the file on server

                  for (Part filePartt : fileParts) {
                         //   String fileName = Paths.get(filePart.getSubmittedFileName()).getFileName().toString(); // MSIE fix.
                            InputStream fileContent = filePartt.getInputStream();

                            // ... (do your job here)
                          PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
                          if (i< inst.length){
                          pr.setString(1, inst[i]);
                    System.out.println("numero "+i);
                          }
                            i++;


                  if (fileContent != null) {
                        // fetches input stream of the upload file for the blob column
                        pr.setBlob(2, fileContent);
                    }
                  pr.setInt(3, idsub);
                  pr.executeUpdate();

                  }



            }
            catch(Exception e){}`

Upvotes: 0

rhenesys
rhenesys

Reputation: 184

You have 2 nested for loops. The first one runs the whole length from inst array and for each one of that you insert all the parts. So if inst has a length of 3 and you have 3 parts you are inserting 3 parts each time the outside loop goes +1. If you know the order from the list is correct, you could do something like:

// ... (do your job here)
        Class.forName("com.mysql.jdbc.Driver");
        java.sql.Connection cnn = DriverManager.getConnection("jdbc:mysql://localhost:3306/digitalrecipe","root","");

for(int i = 0; i < fileParts.size(); i++) {
        Part p = fileParts.get(i);
        InputStream fileContent = p.getInputStream();


        PreparedStatement pr = (PreparedStatement) cnn.prepareStatement("insert into instruction (inst,photo,idsubc) values (?,?,?) ");
        pr.setString(1, String.valueOf(i));
        System.out.println("numero "+i);
        if (fileContent != null) {
            // fetches input stream of the upload file for the blob column
            pr.setBlob(2, fileContent);
        }
        // where idsub comes from??
        pr.setInt(3, idsub);
        pr.executeUpdate();

    }

I tried not changing much from your code, but starting a connection in the for loop seems not right :)

Upvotes: 1

Related Questions