user3778684
user3778684

Reputation: 45

i am trying to use two while loop in a single program to iterate two queries but first while loop is not executed

    public class JustTest  
    {  
        public static void main(String[] args)   
        {            
            //Blank workbook  
            XSSFWorkbook workbook = new XSSFWorkbook();   
            String url ="";
            String username ="";
            String password ="";
               try  
               {  
                   Class.forName("oracle.jdbc.driver.OracleDriver");  
                   Connection c = DriverManager.getConnection(url,username,password);          
                   c.setAutoCommit(false);  
                   System.out.println("Opened database successfully");  

            //Create a blank sheet  
            XSSFSheet sheet = workbook.createSheet("total");  
            Row headerRow = sheet.createRow(0);
            Cell date = headerRow.createCell(0);
            date.setCellValue("date");
            Cell byDate = headerRow.createCell(1);
            byDate.setCellValue("total au");
            Cell byDate1 = headerRow.createCell(1);
            byDate1.setCellValue("total au_dt");
            Statement stmt =  c.createStatement(); 
//passing two queries rs and rs1 
            ResultSet rs = stmt.executeQuery("select empname,empno from emp");
            ResultSet rs1=stmt.executeQuery("select deptno,deptname from dept");
            int row = 1;
//first query iterate
            while(rs.next()) {

                String empname= rs.getString("empname");

                int empno = rs.getInt("empno ");
                Row dataRow = sheet.createRow(row);

                Cell dataNameCell = dataRow.createCell(0);
                dataNameCell.setCellValue(dateValue);

                Cell dataAddressCell = dataRow.createCell(1);
                dataAddressCell.setCellValue(empno );
                row = row + 1;
            }
//second query iterate            
            while(rs1.next()) {
                String deptno=rs1.getString("deptno");
                String deptname =rs1.getString("deptname ");
                Row dataRow = sheet.createRow(row1);

                Cell dataNameCell1 = dataRow.createCell(2);
                dataNameCell1.setCellValue(deptno);

                Cell dataNameCell2 = dataRow.createCell(3);
                dataNameCell2.setCellValue(deptname );


                row1 = row1 + 1;
            }

               }  
         catch ( NullPointerException e )   
                    {  
                     System.out.println( " is not updated because cells cannot be left null/empty ");       
                    }  

          catch ( Exception e )   
                       {  
                         System.err.println( e.getClass().getName()+": "+ e.getMessage() );  
                         System.exit(0);         
                       }  

            try  
            {  
                //Write the workbook in file system  
                FileOutputStream out = new FileOutputStream(new File("C:\\Users\\jit\\Desktop\\allSheets\\justtest.xlsx"));  
                workbook.write(out);  
                out.close();  
                System.out.println("sheet.xlsx written successfully on disk.");  
            }   
            catch (Exception e)   
            {  
                e.printStackTrace();  
            }  
        }  
    } 

    java jdbc jdbc-odbc 

there are two queries and i am trying to iterate the queries using two while loop ,but for me i am always fetching the data of 2nd loop from db..where as first loop is not executed so data's are not stored for first loop..(storing data in an excel sheet) ..can anyone please edit the code so that data can be stored for both the loops..

Upvotes: 0

Views: 557

Answers (1)

CocoNess
CocoNess

Reputation: 4222

Move your second executeQuery to after you process your first while loop. You're probably overwriting

        ResultSet rs = stmt.executeQuery("select empname,empno from emp");
                    int row = 1;
        //first query iterate
        while(rs.next()) {

            String empname= rs.getString("empname");

            int empno = rs.getInt("empno ");
            Row dataRow = sheet.createRow(row);

            Cell dataNameCell = dataRow.createCell(0);
            dataNameCell.setCellValue(dateValue);

            Cell dataAddressCell = dataRow.createCell(1);
            dataAddressCell.setCellValue(empno );
            row = row + 1;
        }
        ResultSet rs1=stmt.executeQuery("select deptno,deptname from dept");

Upvotes: 2

Related Questions