2017-04-25 3 views
0

誰かが私が間違っていることを知っている人はいますか? は私が書き込みした後にどのようなファイルの 例があるFile.xlsxに(ログイン、パスワード)ファイルの最初の項目.xslxがJavaを使用して読み取られていません

Login1 | Password1 
Login2 | Password2 
Login3 | Password3 

をデータベース接続を作成し、データを保存した後、それをテストにファイルから値を取得します。しかし、テストは

Login1 | Password1 

ながら

Login2 | Password2 
Login3 | Passrowd3 

からのデータのダウンロードを開始ファイルからデータをダウンロードするときに省略されています。

私が間違っている箇所に誰かを案内することはできますか?私は初心者です。醜いコードには申し訳ありません。あなたのreadExcelData方法で

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

、あなたはファイルの行の上にループの前にrowIterator.next();を呼び出す:

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

ことコールは冗長で、最初の行をスキップするように見えます。最初のrowIterator.next();コールを削除してみてください。

関連する問題