无法在单个函数中迭代两个while循环

时间:2012-12-12 06:44:21

标签: java jdbc

第一个while循环只迭代一个记录然后另一个循环get循环遍历多个记录但是第一个while循环没有得到对第二个记录的引用或者进一步。我使用两个不同的结果集对象和单个语句对象我做错了请回复并提前感谢。

   public void generateCsvFile()
   {
       String vSQL = "";
       String mSQL = "";
       String mDealer_id = "";
        try
        {
            //mSQL = "mSQL"
            mSQL = "select distinct em_controller_id as dealer_id from entity_master";
            System.out.println("mSQL :: \n"+mSQL);
            rs1 = stmt.executeQuery(mSQL);

            while(rs1.next())
            {           

                //System.out.println("@#@# Dealer id @##@#::"+rs1.getString("dealer_id"));
                mDealer_id = rs1.getString("dealer_id");
                vSQL = "select e.em_entity_id,e.em_name,e.em_address_line_1,e.em_address_line_2,e.em_address_line_3,e.em_phone_no_1,e.em_phone_no_2,e.em_dob,e.em_pan_no,e.em_exch_client_id from entity_master e where e.em_controller_id='"+mDealer_id+"'";   
                System.out.println("vSQL :: \n"+vSQL);
                rs = stmt.executeQuery(vSQL);

                FileWriter writer = new FileWriter(mDealer_id+".csv");

                while(rs.next())
                {

                    System.out.println("em_entity_id !@!@!('"+mDealer_id+"') :: "+rs.getString("em_entity_id"));
                    System.out.println("ENTITY :: "+rs.getString("em_entity_id"));
                    writer.append(rs.getString("em_entity_id"));
                    writer.append(',');
                    writer.append(rs.getString("em_name"));
                    writer.append(',');
                    writer.append(rs.getString("em_address_line_1"));
                    writer.append(',');
                    writer.append(rs.getString("em_address_line_2"));
                    writer.append(',');
                    writer.append(rs.getString("em_address_line_3"));
                    writer.append(',');
                    writer.append(rs.getString("em_phone_no_1"));
                    writer.append(',');
                    writer.append(rs.getString("em_phone_no_2"));
                    writer.append(',');
                    writer.append(rs.getString("em_dob"));
                    writer.append(',');
                    writer.append(rs.getString("em_pan_no"));
                    writer.append(',');
                    writer.append(rs.getString("em_exch_client_id"));
                    writer.append('\n');
                }
                rs.close();
//              //generate whatever data you want
                writer.flush();
                writer.close();

            }
            rs1.close();

        }
        catch(Exception e)
        {
             e.printStackTrace();
        }
    }

2 个答案:

答案 0 :(得分:4)

我怀疑这是问题所在:

rs1 = stmt.executeQuery(mSQL);
// later...
rs = stmt.executeQuery(vSQL);

对于两个并发查询,您似乎使用相同的Statement。那次失败我并不感到惊讶。使用两个单独的Statement变量 - 理想情况下,在您的方法中声明的变量。也:

  • 您还应关闭finally块内的所有内容,以便即使抛出异常,您仍然可以关闭它们。
  • 您应该使用参数化SQL而不是像这样的代码:

    where e.em_controller_id='"+mDealer_id+"'"
    
  • 您应该使用连接而不是执行N + 1个查询

答案 1 :(得分:2)

来自Java API:

  

默认情况下,每个Statement对象只能有一个ResultSet对象   同时打开。因此,如果读取一个ResultSet   对象与另一个对象的读取是交错的,每个都必须已经存在   由不同的Statement对象生成。所有的执行方法都在   Statement接口隐式关闭一个statment的当前ResultSet   对象,如果存在开放的对象。

http://docs.oracle.com/javase/1.4.2/docs/api/java/sql/Statement.html