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



  • 相关阅读:
    AOP在Spring Boot中如何使用
    拦截器在Spring Boot中如何使用
    跨域在Spring Boot中如何处理
    @ControllerAdvice
    文件上传之Spring Boot整合web层
    Git和GitHub
    Spring Boot 整合web层之JSON,gson,fastjson的使用
    Spring boot整合视图层
    Spring Boot中的parent是什么?
    网页自动化,验证码识别函数,深度学习训练
  • 原文地址:https://www.cnblogs.com/zhongmeizhi/p/6306921.html
Copyright © 2020-2023  润新知