开发者

nested loop wont execute queries in internal loop with length bigger than 1

Sorry guys i couldn't provide feedback because of my other username and password here, anyways here is the issue:

I'm working on a SOAP based webservice where in a part of it i have to perform some queries on the database using nested loop, the problem is that the inner loop just gets executed for ONE time only, before giving up.This is the code:

for(int i=0; i<selec.length; i++){
  for(int j=0; j<sintom.length;j++){
    var[(i*sintom.length)+j] = 
      "INSERT INTO malattia (nome, eta,  descrizione, sesso, etnia, sintomi) "
      + "VALUES ('" + malattia + "','" + eta + "','" + descrizione + "','" 
      +  sexarra[0] + "','" + selec[i] + "','" + sintom[j] + "')";
  }
}

This is where the queries are supposed to get executed:

if (errore.equals("")) {
  try {
    Statement st = conn.createStatement();
    for(int i=0; i<selec.length; i++){
      for(int j=0;j<sintom.length;j++){
        st.executeUpdate(var[(i*sintom.length)+j]);
      }
    }

What happens is that no matter the size of select it will work fine as long as the length of sintom is 1, larger than 1 and it won't work.

UPDATE

I just used prepared statement but the problem persists, if the length of the inner loop is bigger than 1, it won't work! here is the code to the changes i made:

if (errore.equals("")) {
  try {
    /*  Statement st = conn.createStatement();
        Statement[] si=new Statement[selec.length];
        for(int i=0;i<selec.length;i++)
        si[i]=conn.createStatement();               */
   PreparedStatement ps = conn.prepareStatement(
     "INSERT INTO malattia (nome, eta, descrizione, sesso, etnia, sintomi) 
      values (?, ?, ?, ?, ?, ?)");


     for(int i=0; i<selec.length; i++){
       for(int j=0;j<sintom.length;j++){
         //  si[i].executeUpdate(var[(i*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();
       }
     }
   }  
   //st.executeUpdate(q);
   ps.close();
   conn.close();
   ris = "si";

Complete Server Code:

             public String aggiungi_malattia(String malattia, int eta, String descrizione, String[] sexarra, String[] selec, String[] sintom) {
        String ris = "no";
         String errore = connetti();
         if (errore.equals("")) {
              try {
               PreparedStatement ps = conn.prepareStatement("INSERT INTO malattia (nome, eta, descrizione, sesso, etnia, sintomi) values (?, ?, ?, ?, ?, ?)");
          else {  
            for(int i=0; i<selec.length; i++){
                for(int j=0;j<sintom.length;j++){

                   //  si[i].executeUpdate(var[(i*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]);

   开发者_如何学JAVA                ps.executeUpdate();
                }
               }
            }



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

The server sends back ris which is either si or no depending on the success of the query, here i get si if sintom which is the length of the inner loop to be 1 otherwise i get no I don't know exactly what causes it to respond with a no furthermore this is a SOAP based web-service, i can debug the client part of the system not the server part


After beating my head for hours and hours, the problem laid in the Database!! what happend was that since the column sintomi was a FK referencing a table of Sintomi which was multivalued, it was therefore a primarykey of the table malattia SO the values which i was passing needed to have been already present in the table sintomi so column sintomi could reference it!! Anyways it would have been impossible to understand that from the information i provided, Thanks everybody! as always great, you guys are there when things get outta control.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜