looping problem while appending data to existing text file

Posted by Manu on Stack Overflow See other posts from Stack Overflow or by Manu
Published on 2010-04-08T12:40:59Z Indexed on 2010/04/08 12:43 UTC
Read the original article Hit count: 257

Filed under:
     try {
     stmt = conn.createStatement();
     stmt1 = conn.createStatement();
     stmt2 = conn.createStatement();
     rs = stmt.executeQuery("select cust from trip1");
     rs1 = stmt1.executeQuery("select cust from trip2");
     rs2 = stmt2.executeQuery("select cust from trip3");
      File f = new File(strFileGenLoc);
      OutputStream os = (OutputStream)new FileOutputStream(f,true);
      String encoding = "UTF8";
      OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
      BufferedWriter bw = new BufferedWriter(osw);


 }


       while ( rs.next() ) {

         while(rs1.next()){

              while(rs2.next()){

         bw.write(rs.getString(1)==null? "":rs.getString(1));
             bw.write("\t");
         bw.write(rs1.getString(1)==null? "":rs1.getString(1));
         bw.write("\t");
         bw.write(rs2.getString(1)==null? "":rs2.getString(1));
         bw.write("\t");

         bw.newLine();

              }
         }
     }

 Above code working fine.
 My problem is 
   1. "rs" resultset contains one record in the table
   2. "rs1" resultset contains 5 record in the table
   3. "rs2" resultset contains 5 record in the table

    "rs" data is getting recursive.

    while writing to the same text file , the output i am getting  like 

    1   2    3
    1   12   21
    1   23   25
    1   10   5
    1   8    54

   but i need output like below

    1   2    3
       12   21
       23   25
       10   5
        8    54

  What things i need to change in my code.. Please advice

© Stack Overflow or respective owner

Related posts about java