我试图在一个程序中使用两个while循环来迭代两个查询,但首先是while循环不执行

时间:2014-10-03 11:14:52

标签: java jdbc

    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 

有两个查询,我试图使用两个while循环迭代查询,但对我来说,我总是从db..where获取第二个循环的数据,因为第一个循环没有被执行所以数据不会被存储为第一个循环..(将数据存储在Excel工作表中)..任何人都可以编辑代码,以便可以为两个循环存储数据..

1 个答案:

答案 0 :(得分:2)

在处理第一个while循环后,将第二个executeQuery移动到。你可能会覆盖

        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");