mysql 字段修改_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > mysql 字段修改

mysql 字段修改

 2014/4/3 12:11:09  ning2-eye  程序员俱乐部  我要评论(0)
  • 摘要:publicstaticvoidmain(String[]args){StringfilePath="C:\\Users\\li-ningning\\Desktop\\fnTable.xls.xls";//filePath="C:\\Users\\li-ningning\\Desktop\\master_data_fncrm.xls";Filefile=newFile(filePath);try{FileInputStreamin=newFileInputStream(file)
  • 标签:SQL MySQL
class="java"> 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();
    }

?

发表评论
用户名: 匿名