文件或Json数据转换成sql语句

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.List;

public class SqlScript {
    private final static String DIR_PATH = "/Users/lucax/Documents/";
    private final static String OUT_FILE = System.currentTimeMillis() + ".sql";

    public static void main(String[] args){
        String[] columns = {"name"};
        List<JSONObject> list = getDataSourceByTxt("单位名称.txt",columns);
        writeFile("company_data",list);
    }

    private static List<JSONObject> getDataSourceByTxt(String fileName,String[] columns){
        List<JSONObject> result = new ArrayList<JSONObject>();
        try {
            File file = new File(DIR_PATH+fileName);
            if(!file.exists()){
                return null;
            }
            InputStreamReader read = new InputStreamReader(new FileInputStream(file), "utf-8");// 考虑到编码格式
            BufferedReader bu = new BufferedReader(read);
            String lineText = null;
            int i = 1;
            JSONObject jsonObject = null;
            while ((lineText = bu.readLine()) != null) {
                System.out.println("第" + i + "行数据," + lineText);
                String[] values = lineText.split(",");
                jsonObject = new JSONObject();
                for(String value:values){
                    jsonObject.put(columns[Arrays.asList(values).indexOf(value)],lineText);
                }
                result.add(jsonObject);
                i++;
            }
        } catch (Exception e) {

        }
        return result;
    }

    private static List<JSONObject> getDataSourceByJson(String json){
        List<JSONObject> result = new ArrayList<JSONObject>();
        JSONArray jsonArray = JSONArray.parseArray(json);
        getList(result, jsonArray);
        return result;
    }

    private static void writeFile(String table,List<JSONObject> list){
        try {
            OutputStream out = new FileOutputStream(DIR_PATH + OUT_FILE);
            //建表
            StringBuilder drop = new StringBuilder("DROP TABLE IF EXISTS `" + table + "`;");
            StringBuilder create = new StringBuilder("CREATE TABLE  `" + table + "` ( ");
            for (String key : list.get(0).keySet()) {
                create.append("'" + key + "' varchar(100) DEFAULT NULL,");
            }
            create.append("`flag` varchar(10) DEFAULT NULL ");
            create.append(" ) ENGINE=InnoDB DEFAULT CHARSET=utf8;");
            out.write(drop.toString().getBytes());
            out.write("\r\n".getBytes());// 写入一个换行
            out.write(create.toString().getBytes());
            StringBuilder insert = new StringBuilder("INSERT INTO `" + table + "` ( ");
            for (String key : list.get(0).keySet()) {
                insert.append("'" + key + "',");
            }
            insert.append("`flag`) VALUES ");
            out.write("\r\n".getBytes());// 写入一个换行
            out.write(insert.toString().getBytes());
            int i = 1;
            for (JSONObject jsonObject : list) {
                StringBuilder builder = new StringBuilder("(");
                for (String key : jsonObject.keySet()) {
                    String value = jsonObject.getString(key);
                    builder.append("'" + value + "',");
                }
                if(i == list.size()){
                    builder.append("`1`);");
                }else{
                    builder.append("`1`),");
                }
                out.write("\r\n".getBytes());// 写入一个换行
                out.write(builder.toString().getBytes());
                i++;
            }
            out.close();
        } catch (Exception e) {

        }
    }

    private static String getJson() {
        StringBuilder regionStr = new StringBuilder("[");
        regionStr.append("{\"code\":\"11\",\"name\":\"北京市\",\"children\":[{\"code\":\"1101\",\"name\":\"市辖区\",\"children\":[{\"code\":\"110101\",\"name\":\"东城区\"},{\"code\":\"110102\",\"name\":\"西城区\"},{\"code\":\"110105\",\"name\":\"朝阳区\"},{\"code\":\"110106\",\"name\":\"丰台区\"},{\"code\":\"110107\",\"name\":\"石景山区\"},{\"code\":\"110108\",\"name\":\"海淀区\"},{\"code\":\"110109\",\"name\":\"门头沟区\"},{\"code\":\"110111\",\"name\":\"房山区\"},{\"code\":\"110112\",\"name\":\"通州区\"},{\"code\":\"110113\",\"name\":\"顺义区\"},{\"code\":\"110114\",\"name\":\"昌平区\"},{\"code\":\"110115\",\"name\":\"大兴区\"},{\"code\":\"110116\",\"name\":\"怀柔区\"},{\"code\":\"110117\",\"name\":\"平谷区\"},{\"code\":\"110118\",\"name\":\"密云区\"},{\"code\":\"110119\",\"name\":\"延庆区\"}]}]},");
        regionStr.append("{\"code\":\"12\",\"name\":\"天津市\",\"children\":[{\"code\":\"1201\",\"name\":\"市辖区\",\"children\":[{\"code\":\"120101\",\"name\":\"和平区\"},{\"code\":\"120102\",\"name\":\"河东区\"},{\"code\":\"120103\",\"name\":\"河西区\"},{\"code\":\"120104\",\"name\":\"南开区\"},{\"code\":\"120105\",\"name\":\"河北区\"},{\"code\":\"120106\",\"name\":\"红桥区\"},{\"code\":\"120110\",\"name\":\"东丽区\"},{\"code\":\"120111\",\"name\":\"西青区\"},{\"code\":\"120112\",\"name\":\"津南区\"},{\"code\":\"120113\",\"name\":\"北辰区\"},{\"code\":\"120114\",\"name\":\"武清区\"},{\"code\":\"120115\",\"name\":\"宝坻区\"},{\"code\":\"120116\",\"name\":\"滨海新区\"},{\"code\":\"120117\",\"name\":\"宁河区\"},{\"code\":\"120118\",\"name\":\"静海区\"},{\"code\":\"120119\",\"name\":\"蓟州区\"}]}]},");
        regionStr.append("]");
        return regionStr.toString();
    }

    private static void getList(List<JSONObject> list,JSONArray jsonArray){
        System.out.println(">>>>>>>>>>>>>>>"+jsonArray);
        Iterator iterator = jsonArray.iterator();
        while (iterator.hasNext()){
            JSONObject jsonObject = (JSONObject) iterator.next();
            String childrenKey = null;
            JSONArray children = null;
            Boolean flag = false;
            for(String key:jsonObject.keySet()){
                Object value = jsonObject.get(key);
                if(value instanceof JSONArray){
                    flag = true;
                    childrenKey = key;
                    children = (JSONArray) value;
                }
            }
            jsonObject.remove(childrenKey);
            list.add(jsonObject);
            if(flag) {
                getList(list, children);
            }
        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值