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;
        }
    
    }



  • 相关阅读:
    EIGRP系统复习【转载】
    EIGRP系统复习【转载】
    EIGRP系统复习【转载】
    新CCIE笔记之'口口相传'路由协议
    新CCIE笔记之'口口相传'路由协议
    新CCIE笔记之'口口相传'路由协议
    【★】自制网络心理需求大排名!
    【★】假如人类使用16进制
    【★】假如人类使用16进制
    算法之【大整数乘法】
  • 原文地址:https://www.cnblogs.com/zhongmeizhi/p/6306921.html
Copyright © 2011-2022 走看看