zoukankan      html  css  js  c++  java
  • Selenium自动化-调用Mysql数据库

    上几篇博客发布了几篇Selenium入门知识和进阶,

      现在附上如何 从数据库中取值

        能够逐行取值,并且返回二维数组

    import java.io.FileInputStream;
    import java.io.IOException;
    import java.util.ArrayList;
    import java.util.List;
    
    import org.apache.poi.hssf.usermodel.HSSFWorkbook;
    import org.apache.poi.ss.usermodel.Row;
    import org.apache.poi.ss.usermodel.Sheet;
    import org.apache.poi.ss.usermodel.Workbook;
    import org.apache.poi.xssf.usermodel.XSSFWorkbook;
    
    public class MyExcel {
        public static Object[][] getExcelData(String filePath, String fileName, String sheetName) throws IOException {
            java.io.File file = new java.io.File(filePath + fileName);
            FileInputStream inputStream = new FileInputStream(file);
            Workbook workbook = null;
            String fileExtensionName = fileName.substring(fileName.indexOf("."));
            if (fileExtensionName.equals(".xlsx")) {
                workbook = new XSSFWorkbook(inputStream);
            }
            else if (fileExtensionName.equals(".xls")) {
                workbook = new HSSFWorkbook(inputStream);
            }
            Sheet sheet = workbook.getSheet(sheetName);
            int rowCount = sheet.getLastRowNum() - sheet.getFirstRowNum();
            List<Object> records = new ArrayList<Object>();
            for (int i = 1; i < rowCount + 1; i++) {
                Row row = sheet.getRow(i);
                String fields[] = new String[row.getLastCellNum()];
                for (int j = 0; j < row.getLastCellNum(); j++) {
                    try {
                        fields[j] = row.getCell(j).getStringCellValue();
                    }
                    catch (Exception e) {
                        fields[j] = null;
                    }
                }
                records.add(fields);
            }
            Object[][] results = new Object[records.size()][];
            for (int i = 0; i < records.size(); i++) {
                results[i] = (Object[]) records.get(i);
            }
            return results;
        }
    
    }



  • 相关阅读:
    Spring注解
    [Exception Android 22]
    Android中Word转Html
    [Exception Android 20]
    POI-word转html
    【JS设计模式】装饰者模式
    C语言中的传值调用
    Spring Aop基础总结
    Android开发-状态栏着色原理和API版本号兼容处理
    9.12測试(二)——国际象棋
  • 原文地址:https://www.cnblogs.com/zhongmeizhi/p/6306921.html
Copyright © 2011-2022 走看看