首页 > 数据库 >自动生成sql 这是MySQL例子

自动生成sql 这是MySQL例子

时间:2023-03-27 11:22:44浏览次数:40  
标签:String sql System 例子 taskId MySQL println append splitArray

@Test
public void testGeneSql() {
try {
String ss = getFileContentTwo("D:\test\13json.txt");

        JSONObject jo = JSONObject.parseObject(ss);
        JSONArray ja = jo.getJSONArray("RECORDS");
        System.out.println("JSONArray size = " + ja.size());

        for (int i = 0; i < ja.size(); i++) {
            String content = ja.getJSONObject(i).getString("XML_CONTENT");
            System.out.println("i=" + i + ",size = " + content.length());
            String taskId = ja.getJSONObject(i).getString("RELATION_TASK_ID");
            System.out.println("taskId = " + taskId);
            List<String> splitArray = getSpiltArray(content);
            String sql = buildSql(splitArray, taskId);
            System.out.println(sql);
        }

    } catch (Exception e) {
        System.out.println("error!!!!!!!!!!!");
    }
}

public String getFileContentTwo(String name) throws IOException {
    File file = new File(name);
    // 判断文件是否存在
    if (file.isFile() && file.exists()) {
        // 考虑到编码格式
        InputStreamReader read =
                new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8.toString());
        BufferedReader bufferedReader = new BufferedReader(read);
        StringBuffer stringBuffer = new StringBuffer();
        String lineTxt = null;
        while ((lineTxt = bufferedReader.readLine()) != null) {
            // System.out.println(lineTxt);
            stringBuffer.append(lineTxt);
        }
        read.close();
        return stringBuffer.toString();
    } else {
        System.out.println("该文件不存在");
        return "";
    }
}

//public List<String> buildSql(List<String> splitArray, String taskId) {
public String buildSql(List<String> splitArray, String taskId) {

    StringBuilder strb = new StringBuilder();
    for (int i = 0; i < splitArray.size(); i++) {
        if (0 == i) {
            strb.append("update AUTOMATIC_TASK_DETAIL set xml_content = '").append(splitArray.get(i)).append("where RELATION_TASK_ID='" + taskId + "';\r\n");
        } else {
            strb.append("update AUTOMATIC_TASK_DETAIL set xml_content = '").append(splitArray.get(i)).append("where RELATION_TASK_ID='" + taskId + "';\r\n");
        }

    }
    return strb.toString();

}

public List<String> getSpiltArray(String content) {
    List<String> splitArray = new ArrayList<>();

    int length = content.length();
    if (length <= 3000) {
        splitArray.add(content);
    } else {
        int index = 0;
        while (index < length) {
            String subContent = content.substring(index, index + 3000);
            splitArray.add(subContent);
            index += 3000;
        }
    }

    return splitArray;
}

标签:String,sql,System,例子,taskId,MySQL,println,append,splitArray
From: https://www.cnblogs.com/codeLearn/p/17260931.html

相关文章

  • MS SQL Server 删除重复行数据
    您可以使用以下SQL语句删除MSSQLServer表中重复的行:WITHCTEAS(SELECTROW_NUMBER()OVER(PARTITIONBYcolumn1,column2,...columnNORDERBY(SELECT0))......
  • SQL Server 索引类型及意义
    一、什么是索引拿汉语字典的目录页(索引)打比方:正如汉语字典中的汉字按页存放一样,SQLServer中的数据记录也是按页存放的,每页容量一般为4K。为了加快查找的速度,汉语字(词)典......
  • MySQL联合索引创建规则
    1、索引应该按照最常用于查询的列的顺序创建。这样可以最大程度地提高查询性能。2、如果查询中包含的列与索引中的列顺序不一致,则无法使用索引。因此,如果您有多个查询,每个......
  • SqlServer(七)数据库优化
    1、对查询进行优化,尽量避免全表扫描(select*fromTable),首先考虑在where及OrderBy使用的列加索引。2、尽量避免在where语句中对字段进行null值条件搜索,否则将导致......
  • SqlServer日期格式转换
    常用:SELECTCONVERT(VARCHAR(100),GETDATE(),24)--:10:57:47SELECTCONVERT(VARCHAR(100),GETDATE(),108)--:10:57:49SELECTCONVERT(VARCHAR(100),GETDATE(),......
  • Centos7 安装 mysql 8
    安装1.配置yum仓库#更新密钥rpm-importhttps:/repo.mysql.com/RPM-GPG-KEYmysql-202#安装Mysql8.x版本yum库rpm-Uvhhttps:/dev.mysql.com/get/mysql80-commu......
  • [pymysql]新增数据-手工新增-自动新增-批量执行
    1.手工新增importpymysql#获取连接conn=pymysql.connect(host='10.105.212.1',port=3306,user='root',password='DemoDemo',database='......
  • [pymysql]查询基础
    importpymysql#获取连接conn=pymysql.connect(host='10.105.212.1',port=3306,user='root',password='DemoDemo',database='db',cha......
  • 一般怎么分析一个sql
    explain语句进行分析。还需要进一步分析的话可以进行optimizer_trace,一共prepare、optimizer、execute阶段,主要关注optimizer阶段。key实际使用的索引。key_len使用的......
  • 为啥安装Mysql, 启动不了服务 ?
    安装过程中,总是卡在这,然后我想手动启动,也是报错,有搞过的么?谢谢。   手动也打不开:  ......