Advertisement
Guest User

Untitled

a guest
Mar 8th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 KB | None | 0 0
  1. WebElement searchbox = driver.findElement(By.name("j_username"));
  2. WebElement searchbox2 = driver.findElement(By.name("j_password"));
  3.  
  4.  
  5.  
  6.  
  7. try {
  8.  
  9. FileInputStream file = new FileInputStream(new File("C:\paulo.xls"));
  10. HSSFWorkbook workbook = new HSSFWorkbook(file);
  11.  
  12. HSSFSheet sheet = workbook.getSheetAt(0);
  13.  
  14. for (int i=1; i <= sheet.getLastRowNum(); i++){
  15.  
  16. String j_username = sheet.getRow(i).getCell(0).getStringCellValue();
  17. String j_password = sheet.getRow(i).getCell(0).getStringCellValue();
  18.  
  19.  
  20. searchbox.sendKeys(j_username);
  21. searchbox2.sendKeys(j_password);
  22.  
  23.  
  24. searchbox.submit();
  25.  
  26. driver.manage().timeouts().implicitlyWait(10000, TimeUnit.MILLISECONDS);
  27.  
  28. }
  29.  
  30. workbook.close();
  31. file.close();
  32.  
  33. } catch (FileNotFoundException fnfe) {
  34. fnfe.printStackTrace();
  35. } catch (IOException ioe) {
  36. ioe.printStackTrace();
  37.  
  38. Cell cell = sheet.getRow(i).getCell(0);
  39. cell.setCellType ( Cell.CELL_TYPE_STRING );
  40. String j_username = cell.getStringCellValue();
  41.  
  42. DataFormatter formatter = new DataFormatter(); //creating formatter using the default locale
  43. Cell cell = sheet.getRow(i).getCell(0);
  44. String j_username = formatter.formatCellValue(cell); //Returns the formatted value of a cell as a String regardless of the cell type.
  45.  
  46. // Create a formatter, do this once
  47. DataFormatter formatter = new DataFormatter(Locale.US);
  48.  
  49. .....
  50.  
  51. for (int i=1; i <= sheet.getLastRowNum(); i++) {
  52. Row r = sheet.getRow(i);
  53. if (r == null) {
  54. // empty row, skip
  55. } else {
  56. String j_username = formatter.formatCellValue(row.getCell(0));
  57. String j_password = formatter.formatCellValue(row.getCell(1));
  58.  
  59. // Use these
  60. }
  61. }
  62.  
  63. public static void main(String[] args) throws Exception {
  64.  
  65. try {
  66.  
  67. Class forName = Class.forName("com.mysql.jdbc.Driver");
  68. Connection con = null;
  69. con = DriverManager.getConnection("jdbc:mysql://localhost/tables", "root", "root");
  70. con.setAutoCommit(false);
  71. PreparedStatement pstm = null;
  72. FileInputStream input = new FileInputStream("C:\Users\Desktop\a1.xls");
  73. POIFSFileSystem fs = new POIFSFileSystem(input);
  74. Workbook workbook;
  75. workbook = WorkbookFactory.create(fs);
  76. Sheet sheet = workbook.getSheetAt(0);
  77. Row row;
  78. for (int i = 1; i <= sheet.getLastRowNum(); i++) {
  79. row = (Row) sheet.getRow(i);
  80. String name = row.getCell(0).getStringCellValue();
  81. String add = row.getCell(1).getStringCellValue();
  82.  
  83. int contact = (int) row.getCell(2).getNumericCellValue();
  84.  
  85. String email = row.getCell(3).getStringCellValue();
  86.  
  87. String sql = "INSERT INTO employee (name, address, contactNo, email) VALUES('" + name + "','" + add + "'," + contact + ",'" + email + "')";
  88. pstm = (PreparedStatement) con.prepareStatement(sql);
  89. pstm.execute();
  90. System.out.println("Import rows " + i);
  91. }
  92. con.commit();
  93. pstm.close();
  94. con.close();
  95. input.close();
  96. System.out.println("Success import excel to mysql table");
  97. } catch (IOException e) {
  98. }
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement