由于短时工作需要insert语句

Posted senjiang

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了由于短时工作需要insert语句相关的知识,希望对你有一定的参考价值。

忽略批量生成的前提

暴力书写insertUtil

从navicat 中复制 insert语句 至此传入


import java.util.Arrays;
import java.util.List;

/**
* @author zs
* @date 2019/7/30
*/
public class InsertUtils

public static String createInsertSql(String string)
// 删除所有
final String sub = string.replaceAll("`", "");
StringBuffer stringBuffer = new StringBuffer();
final String subedString = stringBuffer.append(sub.substring(0, sub.indexOf("o") + 1)).append(" ").append(sub.substring(sub.indexOf(".") + 1, sub.indexOf("values")+6)).toString();
String substring = subedString.substring(subedString.indexOf("(")+1, subedString.indexOf(")"));
List<String> list = Arrays.asList(substring.split(","));
String head = subedString.substring(0, subedString.indexOf("values"));
String sqlWord = "";
StringBuffer sb = new StringBuffer();
sb.append(head).append(" ").append("values").append(" ").append("(").append(" ");
for (int i = 0; i < list.size(); i++)
if (list.get(i).contains("_"))
sqlWord = deleteUnderline(list.get(i));
else
sqlWord = list.get(i).trim();

if (i == list.size()-1)
sb.append("#").append(sqlWord).append("").append(" ");
else
sb.append("#").append(sqlWord).append("").append(",").append(" ");


// System.out.println(sb.toString());

sb.append(")").append(";");
return sb.toString();



private static String deleteUnderline(String domian)
StringBuffer sb = new StringBuffer();
final String[] strings = domian.split("_");
for (int i = 0; i < strings.length; i++)
if (i >0)
final char[] chars = strings[i].toCharArray();
chars[0] = (char) (chars[0] - 32);
strings[i] = new String(chars);

sb.append(strings[i]);


return sb.toString().trim();


public static void main(String[] args)
String s ="insert into `closet`.`auth_resource` ( `biz_id`, `res_id`, `res_name`, `res_type`, `parent_res_id`, `res_description`, `res_path`, `res_url`, `remark`, `delete_flag`, `create_user`, `create_time`, `update_user`, `update_time`) values ( ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘, ‘‘);";
String insertSql = InsertUtils.createInsertSql(s);
System.out.println(insertSql);

 

以上是关于由于短时工作需要insert语句的主要内容,如果未能解决你的问题,请参考以下文章

mysql数据库使用insert语句插入中文数据报错

Oracle Merge语句

DB2若何将数据导出为insert语句?

Oracle基础之Merge into

Oracle 使用MERGE INTO 语句更新数据

Oracle 使用MERGE INTO 语句更新数据