嵌套循环不会在内部循环中执行长度大于1的查询

发布于 2024-11-30 20:31:45 字数 3480 浏览 0 评论 0原文

抱歉,我无法提供反馈,因为我在这里有其他用户名和密码,无论如何,问题是:

我正在开发一个基于 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,根据成功情况,它是 sino查询的,这里我得到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

能怎样 2024-12-07 20:31:45

在我绞尽脑汁思考了几个小时之后,问题出在数据库上!发生的情况是,由于 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文