2017-04-25 32 views
0

任何人都可以知道我在做什么错了吗? 我创建一个数据库连接和数据(登录,密码)保存到File.xlsx的是什么文件写入文件.xslx中的第一项不是用java读取的

Login1 | Password1 
Login2 | Password2 
Login3 | Password3 

然后,它从文件到测试检索值之后 实施例。然而,测试开始从

Login2 | Password2 
Login3 | Passrowd3 

虽然

Login1 | Password1 

它从一个文件下载数据时省略下载数据。

有人能指导我哪里出错吗?我是初学者,对于丑陋的代码感到抱歉。

ReadData.java

public class ReadData { 
public ReadData() throws Exception { 
    super(); 
} 

public ArrayList readExcelData(int colNo) throws IOException, InvalidFormatException{ 
    OPCPackage pkg = OPCPackage.open(new File("C:\\Users\\Damian6666\\workspace846\\ChangePasswordAutomat1\\File.xlsx")); 
    XSSFWorkbook wb = new XSSFWorkbook(pkg); 

    XSSFSheet s = wb.getSheet("EMP_DETAILS"); 

    Iterator<Row> rowIterator = s.iterator(); 
    rowIterator.next(); 

    ArrayList<String> list = new ArrayList<String>(); 
    while(rowIterator.hasNext()){ 
     list.add(rowIterator.next().getCell(colNo).getStringCellValue()); 
    } 
    System.out.println("List :::: "+list); 
    return list; 
} 

public void tc() throws IOException, InterruptedException, InvalidFormatException{ 
    WebDriver driver = new FirefoxDriver(); 
    driver.get("https://accounts.google.com/ServiceLogin?service=mail&passive=true&rm=false&continue=https://mail.google.com/mail/&ss=1&scc=1&ltmpl=default&ltmplcache=2&emr=1&osid=1#identifier"); 
    driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); 
    driver.manage().window().maximize(); 

    ArrayList<String> userName = readExcelData(0); 
    ArrayList<String> password = readExcelData(1); 

    for(int i =0;i<userName.size();i++){ 
    // driver.findElement(By.id("f_login")).clear(); 
    driver.findElement(By.id("Email")).sendKeys(userName.get(i)); 
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); 
    driver.findElement(By.id("next")).click(); 
// driver.findElement(By.id("f_password")).clear(); 
    driver.findElement(By.id("Passwd")).sendKeys(password.get(i)); 
    driver.manage().timeouts().implicitlyWait(100, TimeUnit.SECONDS); 

    driver.findElement(By.id("signIn")).click(); 
    driver.findElement(By.xpath(".//*[@id='gb']/div[1]/div[1]/div[2]/div[4]/div[1]/a/span")).click(); 
    driver.findElement(By.id("gb_71")).click(); 
    driver.findElement(By.id("account-chooser-link")).click(); 
    driver.findElement(By.id("account-chooser-add-account")).click(); 

    Thread.sleep(6000); 
    } 
} 

} 

JDBCConnectionDB.java

public class JDBCConnectionDB extends ReadData { 

public JDBCConnectionDB() throws Exception { 
    super(); 
} 



@Test 
public void test() {  

    Connection connection = null; 
    Statement stmt = null; 
    int count =0; 

    try{ 
    // Register JDBC driver 
    Class.forName("org.postgresql.Driver"); 
    System.out.println("PostgreSQL JDBC Driver Registered!"); 

    // Open a connection 
    System.out.println("Connecting to a selected database..."); 
    connection = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "postgres", ""); 
    System.out.println("Connected database successfully..."); 

    // Execute a query 
    System.out.println("Creating statment..."); 
    stmt = connection.createStatement(); 

    // Create 2007 format Workbook and Worksheet objects 
    XSSFWorkbook new_workbook = new XSSFWorkbook(); // create a blank workbook object 
    XSSFSheet sheet = new_workbook.createSheet("EMP_DETAILS"); // create a worksheet with caption score_details 

    // Define the SQL query 
    String sql = "SELECT login, password FROM \"Permissions\""; 
    ResultSet rs = stmt.executeQuery(sql); 

    // Create Map for Excel Data 
    Map<String, Object[]> excel_data = new HashMap<String, Object[]>(); 
    int row_counter = 0; 
    //Extract data from result set 
    while(rs.next()){ 
     row_counter = row_counter+1; 
     String login = rs.getString("login"); 
     String password = rs.getString("password"); 
     excel_data.put(Integer.toString(row_counter), new Object[] {login, password});      
    } 
    rs.close(); 

    // Load data into logical worksheet 
    Set<String> keyset = excel_data.keySet(); 
    int rownum = 0; 
    for(String key : keyset){ // loop through the data and add them to the cell 

     Row row = sheet.createRow(rownum++); 
     Object [] objArr = excel_data.get(key); 
     int cellnum = 0; 
     for(Object obj : objArr){ 
      Cell cell = row.createCell(cellnum++); 
      if(obj instanceof Double) 
       cell.setCellValue((Double)obj); 
      else 
       cell.setCellValue((String)obj); 
     } 
    } 
    FileOutputStream output_file = new FileOutputStream(new File("File.xlsx")); // create XLSX file 
    new_workbook.write(output_file); // write excel document to output stream 
    output_file.close(); // close the file 

    ReadData data = new ReadData(); 
    data.tc(); 

    }catch(SQLException se){ 
     // Handle errors for JDBC 
     se.printStackTrace(); 
    }catch (Exception e) { 
     // Handle errors for Class.forName 
     e.printStackTrace(); 
    }finally { 
     // finally block used to close resources 
     try { 
      if(stmt!=null) 
       connection.close(); 
     } catch (SQLException se) { 
      // TODO: handle exception 
     }//do nothing 
     try { 
      if(connection!=null) 
       connection.close(); 
     } catch (SQLException se) { 
      se.printStackTrace();  
     }//end finally try 
    } // end try    
    System.out.println("Goodbye"); 
} 
} //end JDBCConnectionDB*/ 

回答

0

在你readExcelData方法,您在遍历文件的行之前调用rowIterator.next();

Iterator<Row> rowIterator = s.iterator(); 
rowIterator.next(); 

那调用似乎是多余的,看起来会跳过第一行。所以尝试,删除第一个rowIterator.next();电话。