将Resultset内容复制到arraylist并比较这两个值

时间:2013-08-27 09:30:50

标签: java sql jdbc db2

在下面的代码中,我将resultset内容复制到arraylist。 wile循环的第一部分即while(RS.next())正在重新调整结果,但是当光标移动到时 接下来循环即while(SR.next())我得到“结果集已关闭”。请帮助我在哪里犯错误。

       String SSQ =   "select DISTINCT S_NUMBER from OTG.S_R_VAL" +

                    "  WHERE R_TS = (SELECT MAX(R_TS) FROM OTG.S_R_VAL) order by S_NUMBER";

              String SDS = "SELECT DISTINCT S_NUMBER FROM OTG.S_R_VAL AS STG WHERE S_NUMBER NOT IN" +

                            "(SELECT S_NO FROM OTG.R_VAL AS REV WHERE STG.S_NUMBER = REV.S_NO )";

              String SSR = "SELECT DISTINCT S_NO FROM OTG.R_VAL where S_NO != 'NULL' order by S_NO";

              String SSO =  "Select O_UID from OTG.OPTY where C_S_NO IN" +

             "( SELECT DISTINCT S_NUMBER FROM OTG.S_R_VAL AS STG WHERE S_NUMBER NOT IN(SELECT S_NO FROM OTG.R_VAL AS REV WHERE STG.S_NUMBER = REV.S_NO ))";

              //Statement statement;

            try {

         connection = DatabaseConnection.getCon();

         statement = connection.createStatement();

         statement1 = connection.createStatement();

         statement2 = connection.createStatement();

         statement3 = connection.createStatement();

         statement4 = connection.createStatement();



      ResultSet RS = statement1.executeQuery(selectQuery);

      ResultSet DS = statement2.executeQuery(Distinct_SiebelNo);

      ResultSet SR = statement3.executeQuery(SiebelNo_Rev);

      ResultSet SO = statement4.executeQuery(selected_OppId);




      ArrayList<String> RSList = new ArrayList<String>(); 

      ArrayList<String> SRList = new ArrayList<String>();

     /* ResultSetMetaData resultSetMetaData = RS.getMetaData();

      int count = resultSetMetaData.getColumnCount();*/

     int count=1;

      System.out.println("******count********"+count); 

      while(RS.next()) {

          int i = 1;

          count=1;

          while(i < count) 

                      {

              RSList.add(RS.getString(i++));

                  }

          System.out.println(RS.getString("SIEBEL_NUMBER"));


          RSList.add(  RS.getString("SIEBEL_NUMBER"));


        }

    /*  ResultSetMetaData resultSetMetaData1 = SR.getMetaData();

      int count1 = resultSetMetaData1.getColumnCount();*/

      int count1=1;

      while(SR.next()) {

          int i = 1;

          while(i < count1) 

                      {

              SRList.add(SR.getString(i++));

                  }

          System.out.println(SR.getString("SIEBEL_NO"));

          SRList.add(  SR.getString("SIEBEL_NO"));


        }SR.close();

    connection.commit();


} catch (SQLException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }
  }
}
}

2 个答案:

答案 0 :(得分:0)

您在第二个循环(RS行)中使用第一个结果集(System.out.println

答案 1 :(得分:0)

每个循环的逻辑都是有缺陷的。

  int count=1;//Count is being set to one
  while(RS.next()) {
      int i = 1;//i is being set to one
      count=1;//count again set to one
      while(i < count) //condition will always fail as one is never less than one
      {
          RSList.add(RS.getString(i++));//Code is never Reached

      }
      System.out.println(RS.getString("SIEBEL_NUMBER"));


      RSList.add(  RS.getString("SIEBEL_NUMBER"));


    }

不需要第二次。只需使用:

   int count = 1;
   while(RS.next()) {
      RSList.add(RS.getString(count++));
      System.out.println(RS.getString("SIEBEL_NUMBER"));
      RSList.add(  RS.getString("SIEBEL_NUMBER"));
    }

修改

   int count1=1;
   while(SR.next()) {
      SRList.add(SR.getString(count1++));
      System.out.println(SR.getString("SIEBEL_NO"));
      SRList.add(  SR.getString("SIEBEL_NO"));
   }

编辑2:

for (String s : RSList)
   for(String s1 : SRList)
      if (s.equals(s1))
         //Do what you need