嵌套循环不会在内部循环中执行长度大于1的查询
抱歉,我无法提供反馈,因为我在这里有其他用户名和密码,无论如何,问题是:
我正在开发一个基于 SOAP 的 Web 服务,其中一部分我必须使用嵌套循环对数据库执行一些查询,问题是内部循环在放弃之前只执行一次。这是代码:
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] + "')";
}
}
这是应该执行查询的地方:
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]);
}
}
发生的情况是,无论 select< 的大小/code> 只要长度足够,它就可以正常工作sintom 为 1,大于 1 则不起作用。
更新
我只是使用了准备好的语句,但问题仍然存在,如果内循环的长度大于1,它将不起作用!这是我所做的更改的代码:
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";
完整的服务器代码:
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]);
ps.executeUpdate();
}
}
}
//st.executeUpdate(q);
ps.close();
conn.close();
ris = "si";
} catch (SQLException e) {
System.out.println("Errore: " + e.getMessage());
return ris;
}
}
return ris;
}
服务器发送回 ris,根据成功情况,它是 si 或 no查询的,这里我得到 si if sintom 这是内循环的长度为 1 否则我得到 no 我不确切地知道是什么导致它以否响应此外,这是基于 SOAP 的 Web 服务,我可以调试系统的客户端部分而不是服务器部分
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]);
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我绞尽脑汁思考了几个小时之后,问题出在数据库上!发生的情况是,由于 sintomi 列是引用多值 Sintomi 表的 FK,因此它是 malattia 表的主键,因此我传递的值需要已经存在于表 sintomi 所以列 sintomi 可以引用它!!无论如何,从我提供的信息来看是不可能理解的,谢谢大家!一如既往的好,当事情失控时,你们就在身边。
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.