0

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.

10
  • Can you format your code correctly so we could read it better? Also, are you saying that there is an exception thrown? In that case, please also put the exception as part of the question. Commented Aug 25, 2011 at 2:33
  • 1
    And 0 accepted answer out of 8 is not very inspiring. Commented Aug 25, 2011 at 2:37
  • @momo i only know that an exception is thrown because the catch(SQLException e) is activated but i don't exactly see the exception since this code is running on the server side of the webservice, only "ris" is returned to the client side. Commented Aug 25, 2011 at 2:55
  • @Nikita, I got some issues with registration and usernames, Almost all the questions are old when i first started using the site, didn't know how it works BUT i did gave feedback on how i ultimately solved the problem, now I'm once again using the old username, I still don't have the rights to select my own answer as a solution. Commented Aug 25, 2011 at 3:02
  • running time of O(n^3). Its terrible I believe. but seems like u need it :( Commented Aug 25, 2011 at 3:03

2 Answers 2

1

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

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

3 Comments

I need the preparedstatement to run (sintom.lenghselec.length)*sexarra.length times where sexarra.length==2. but the outter loop would run only half that times(sintom.lengthselec.length)*1 times.
My suggestion was to close and load the prepared statement after every ps.update.
Thank you, but i resolved the problem. I can't mark it as answered till tomorrow, coz it won't allow me to do so.
0

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.

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.