三级嵌套循环查询不会运行外循环
我有 3 级嵌套循环,它应该执行一个查询,是一个更大的 web 服务的一部分,但问题是最外面的循环只执行一半的代码,如下所示:
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;
}
问题出在第一部分,其中 sexarra. lengh=2
,正是在最外层循环中,应该迭代两次,但是一旦完成与 k=0 相关的循环,哪个程序就会抛出异常,我的意思是它不执行循环相关于K=1
。我几天来一直遇到嵌套循环和 preparedstatement 的问题,这是最新的一个,我不知道我做错了什么。感谢大家的时间和努力。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我还没有尝试你的程序的逻辑,但我建议你看看重用一个PreparedStatement多次次
我从来没有使用过在不再次抓取实例的情况下重置prepared statements参数的情况(可以使用poolpreparedstatement来缓存statement)。这可能是这里的问题
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
我通过查看服务器日志找到了答案,由 @Stephen C 在评论中建议,该错误是由于拒绝执行查询而引起的,外部循环传递了不同的值,而其他值保持不变,但值已传递外部不包含表的主键,因此,查询实际上是重复的。
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.