Home > Database > Mysql Tutorial > mysql 字段批改

mysql 字段批改

WBOY
Release: 2016-06-07 16:25:17
Original
1095 people have browsed it

mysql 字段修改 public static void main(String[] args) { String filePath = "C:\\Users\\li-ningning\\Desktop\\fnTable.xls.xls";// filePath = "C:\\Users\\li-ningning\\Desktop\\master_data_fncrm.xls"; File file = new File(filePath); try { FileI

mysql 字段修改
 public static void main(String[] args) {
        String filePath = "C:\\Users\\li-ningning\\Desktop\\fnTable.xls.xls";
//        filePath = "C:\\Users\\li-ningning\\Desktop\\master_data_fncrm.xls";
        File file = new File(filePath);
        try {
            FileInputStream in = new FileInputStream(file);
            HSSFWorkbook wb = new HSSFWorkbook(in);
            // /获取第一张Sheet表
            HSSFSheet st = wb.getSheetAt(0);
            for (int i = 0; i <= st.getLastRowNum(); i++) {
                String sql = "alter table ";
                HSSFRow row = st.getRow(i);
                if (row != null) {
                    String tableName = toString((HSSFCell) row
                            .getCell((short) 0));
                    String column = toString((HSSFCell) row.getCell((short) 1));
                    String length = toString((HSSFCell) row.getCell((short) 3));
                    int value = Integer.parseInt(length);
                    sql += tableName;
                    sql += " modify ";
                    sql += column;
                    if (value <= 50) {
                        sql += "  varchar(50)";
                    } else if (value <= 100) {
                        sql += "  varchar(100)";
                    } else if (value <= 150) {
                        sql += "  varchar(150)";
                    } else if (value <= 200) {
                        sql += "  varchar(200)";
                    } else if (value <= 250) {
                        sql += "  varchar(250)";
                    } else if (value <= 300) {
                        sql += "  varchar(300)";
                    } else if (value < 500) {
                        sql += "  varchar(500)";
                    }
                    sql += ";";
                    String executeSql = sql;
                    System.out.println(executeSql);
                    sql = "";
                }
            }
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String toString(HSSFCell cell) {
        String value = "";
        if (cell == null) {
            return value;
        }
        cell.setEncoding(HSSFCell.ENCODING_UTF_16);
        switch (cell.getCellType()) {
        case HSSFCell.CELL_TYPE_STRING:
            value = cell.getStringCellValue().toString();
            break;
        case HSSFCell.CELL_TYPE_NUMERIC:
            if (HSSFDateUtil.isCellDateFormatted(cell)) {
                Date date = cell.getDateCellValue();
                if (date != null) {
                    value = new SimpleDateFormat("yyyy-MM-dd").format(date);
                } else {
                    value = "";
                }
            } else {
                value = new DecimalFormat("0").format(cell
                        .getNumericCellValue());
            }
            break;
        case HSSFCell.CELL_TYPE_FORMULA:
            // 导入时如果为公式生成的数据则无值
            if (!cell.getStringCellValue().equals("")) {
                value = cell.getStringCellValue();
            } else {
                value = cell.getNumericCellValue() + "";
            }
            break;
        case HSSFCell.CELL_TYPE_BLANK:
            break;
        case HSSFCell.CELL_TYPE_ERROR:
            value = "";
            break;
        case HSSFCell.CELL_TYPE_BOOLEAN:
            value = (cell.getBooleanCellValue() ? "YES" : "NO");
            break;
        default:
            value = "";
        }
        return value.trim();
    }
Copy after login

?

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template