tutak
tutak

Reputation: 1098

3 level nested loop query wont run outer loop

I have 3 level nested loop which is supposed to execute a query, part of a bigger webservice but the issue is that the outermost loop is executed only half the code is as follow:

       PreparedStatement ps = conn.prepareStatement("INSERT INTO malattia (nome, eta, descrizione, sesso, etnia, sintomi) values (?, ?, ?, ?, ?, ?)");


              if(sexarra.length == 2){

                  for(int k=0; k<sexarra.length; k++)  {
                    for(int i=0; i<selec.length; i++){
                      for(int j=0;j<sintom.length;j++){

                        ps.setString(1, malattia);
                        ps.setInt(2, eta);
                        ps.setString(3, descrizione);
                        ps.setString(4, sexarra[k] );
                        ps.setString(5, selec[i]);
                        ps.setString(6, sintom[j]);
                        ps.executeUpdate();
                      }
                    }
                  }
             }
              else {

                  for(int i=0; i<selec.length; i++){
                        for(int j=0;j<sintom.length;j++){

                        ps.setString(1, malattia);
                        ps.setInt(2, eta);
                        ps.setString(3, descrizione);
                        ps.setString(4, sexarra[0] );
                        ps.setString(5, selec[i]);
                        ps.setString(6, sintom[j]);
                        ps.executeUpdate();

                       }

                  }                   
                 }

                ps.close();
                 //ds.close();
                conn.close();
                ris = "si";
              } catch (SQLException e) {
                    System.out.println("Errore: " + e.getMessage());

                    return ris;
              }
        }
        return ris;
}

The problem lays in the first part where sexarra.lengh=2, precisely in the most outer loop which is supposed to iterate for two times, but what program throws an exception as soon the loops related to k=0 are done, I mean it doesn't execute the loops related to K=1. I have been having problems with nested loops and preparedstatement for days now and this is the latest one, where I don't know what am I doing wrong. Thanks for your time and effort people.

Upvotes: 0

Views: 593

Answers (2)

tutak
tutak

Reputation: 1098

I found the answer by looking into server logs, advised by @ Stephen C in the comments, the error was caused due to the refusal of execution of query, the outter loop was passing different value while the other values remained the same, but value passed by the outter did not consist Primary Key of the table therefore, the query was effectively duplicate.

Upvotes: 0

Fazal
Fazal

Reputation: 3051

I am yet to try the logic of your program, but I would suggest you look at Reusing a PreparedStatement multiple times

I have never used the case of resetting prepared statement parameters without grabbing the instance again (poolpreparedstatement can be used to cache the statement). That might be the issue here

Upvotes: 1

Related Questions