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.