求解 关于java 字符串操作问题

加贝先生 发布于 2016/10/14 09:42
阅读 142
收藏 0
`2016-09-22 11:09:23,`wxb12b0b70efada027,`1374864402,`0,`,`4001312001201609224600928240,`14745137559311W0RKs6Zb,`oLKWvwnbqnnFG0D2MuR3XIzJwuBs,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~
`2016-09-22 11:05:37,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224600765713,`14745135295846tZQvrL96,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~
`2016-09-22 15:45:46,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624248784,`14745303372997XwWKUTmB,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~
`2016-09-22 15:38:35,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624469066,`14745299049539zH7CIqYR,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~
`2016-09-22 15:48:56,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624837680,`147453052657855c2e22dr,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~
`2016-09-22 15:58:58,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224625173896,`14745311296774lQZoMr9H,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~
`2016-09-22 11:03:29,`wxb12b0b70efada027,`1374864402,`0,`,`4005362001201609224600140554,`14745133566959Fb4unQbY,`oLKWvwjBX8iC3jdpScA0kY53HaG8,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~

`2016-09-22 10:56:04,`wxb12b0b70efada027,`1374864402,`0,`,`4005402001201609224599871500,`147451295558745Lkp7igi,`oLKWvwnrRBnJnIuBokb_yehEP-as,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~



我有几条这样的数据 我想循环 拼接 成 多条 insert 语句    怎么操作?

加载中
0
szwx855
szwx855

引用来自“szwx855”的评论

进UE,选择列模式,在这些数据前面加上INSERT INTO TABLE ('ID','NAME','AGE') VALUES (你的数据)

就好了。

你的是MYSQL.可以通过IDE直接导出INSERT的,不用这样搞。

package com.linjian.TetMatch;

import org.apache.commons.lang.StringUtils;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 XXX技术有限公司 版权所有
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */
public class TestMatch {


    public static String genInsert(String cols[],String data[]) {

        StringBuilder sb = new StringBuilder("INSERT INTO TABLE ");

        sb.append("(");
        sb.append(StringUtils.join(cols, ","));
        sb.append(")");
        sb.append(" VALUES ");
        sb.append("(");
        for (String dataInfo : data) {
            sb.append("'");
            sb.append(dataInfo);
            sb.append("',");
        }
        sb = new StringBuilder(sb.substring(0, sb.length() - 1));
        sb.append(")");
        return sb.toString();
    }

    public static void main(String[] args) {

        String a = "`2016-09-22 11:09:23,`wxb12b0b70efada027,`1374864402,`0.00,`0.10%~";
        String data[] = a.replaceAll("`", "").split(",");
        String colsName[] = { "time","sn","mobile","value","pence" };
        String result = genInsert(colsName, data);
        System.out.println(result);


    }
}


========================结果===========================
INSERT INTO TABLE (time,sn,mobile,value,pence) VALUES ('2016-09-22 11:09:23','wxb12b0b70efada027','1374864402','0.00','0.10%~')

Process finished with exit code 0



加贝先生
还有一个问题我想问一下 我要怎么判断 拼接的时候 是拼接的第一个字段 我第一个字段是时间的 要弄成 to_date('2016-01-01','yyyy-MM-dd') 要格式化一下
加贝先生
感谢你的回答 根据你的代码我解决了问题
加贝先生
我现在是主要不知道多条数据怎么弄 单挑的弄出来了
0
szwx855
szwx855

进UE,选择列模式,在这些数据前面加上INSERT INTO TABLE ('ID','NAME','AGE') VALUES (你的数据)

就好了。

你的是MYSQL.可以通过IDE直接导出INSERT的,不用这样搞。

加贝先生
这个是接口 返回的 字符串 我的意思是用 java split 拼接的方式 拼接insert语句
0
就是个精虫上脑的地方
就是个精虫上脑的地方
用mybatis 把字符串解析成多个DO 
加贝先生
没用这个框架
0
IdleMan
IdleMan
editplus你值得拥有
0
kukudeku
kukudeku

package org.test;

public class StitchString {
 private static final String TABLE_FIELDS = "OPR_DATE,ID,PHONE,FIELD4,FIELD5,FIELD6,FIELD7,FIELD8";
 private static final String TABLE_NAME = "YOU_TABLE_NAME";
 public static void main(String[] args) {
  StringBuffer  sb = new StringBuffer();
  sb.append("INSERT INTO "+TABLE_NAME+" ("+TABLE_FIELDS+") VALUES ");
  String getString = new StitchString().new StringClass().getString();
//  System.out.println("获取到的字符串:\n"+getString);
  //利用\n获取字符串数组
  String[] rowStrings = getString.split("\n");
  for(int i = 0;i<rowStrings.length;i++){
   //将`取消
   String rowString = rowStrings[i];
   rowString = rowString.replaceAll("`", "");
//   System.out.println("取消`之后得字符串"+rowString);
   //利用,分割字符串
   StringBuffer finalString = new StringBuffer();
   String[] strSplit = rowString.split(",");
   for (int j=0;j<strSplit.length;j++) {
    //给每个字段添加单引号
//    System.out.println("添加引号之前:"+strSplit[j]);
    strSplit[j] = "'" + strSplit[j] + "'";
//    System.out.println("添加引号之后:"+strSplit[j]);
    finalString.append(strSplit[j]);
    if(j != strSplit.length -1){
     finalString.append(",");
    }
   }
   sb.append("(");
   sb.append(finalString);
   sb.append(")");
   if(i != rowString.length() - 1){
    sb.append(",");
   }
  }
  String sql_insert = sb.toString();
  System.out.println("插入语句"+sql_insert);
 }
 public interface StringIntf {
  public String getString();
 }
 public class StringClass implements StringIntf{

  @Override
  public String getString() {
   // TODO Auto-generated method stub
   StringBuffer sb = new StringBuffer();
   sb.append("`2016-09-22 11:09:23,`wxb12b0b70efada027,`1374864402,`0,`,`4001312001201609224600928240,`14745137559311W0RKs6Zb,`oLKWvwnbqnnFG0D2MuR3XIzJwuBs,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 11:05:37,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224600765713,`14745135295846tZQvrL96,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:45:46,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624248784,`14745303372997XwWKUTmB,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:38:35,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624469066,`14745299049539zH7CIqYR,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:48:56,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624837680,`147453052657855c2e22dr,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:58:58,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224625173896,`14745311296774lQZoMr9H,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 11:03:29,`wxb12b0b70efada027,`1374864402,`0,`,`4005362001201609224600140554,`14745133566959Fb4unQbY,`oLKWvwjBX8iC3jdpScA0kY53HaG8,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 10:56:04,`wxb12b0b70efada027,`1374864402,`0,`,`4005402001201609224599871500,`147451295558745Lkp7igi,`oLKWvwnrRBnJnIuBokb_yehEP-as,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   System.out.println("要返回的字符串:\n"+sb.toString());
   return sb.toString();
  }
 }
}

运行结果:

插入语句INSERT INTO YOU_TABLE_NAME (OPR_DATE,ID,PHONE,FIELD4,FIELD5,FIELD6,FIELD7,FIELD8) VALUES ('2016-09-22 11:09:23','wxb12b0b70efada027','1374864402','0','','4001312001201609224600928240','14745137559311W0RKs6Zb','oLKWvwnbqnnFG0D2MuR3XIzJwuBs','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),('2016-09-22 11:05:37','wxb12b0b70efada027','1374864402','0','','4003722001201609224600765713','14745135295846tZQvrL96','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),('2016-09-22 15:45:46','wxb12b0b70efada027','1374864402','0','','4003722001201609224624248784','14745303372997XwWKUTmB','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 15:38:35','wxb12b0b70efada027','1374864402','0','','4003722001201609224624469066','14745299049539zH7CIqYR','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 15:48:56','wxb12b0b70efada027','1374864402','0','','4003722001201609224624837680','147453052657855c2e22dr','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 15:58:58','wxb12b0b70efada027','1374864402','0','','4003722001201609224625173896','14745311296774lQZoMr9H','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 11:03:29','wxb12b0b70efada027','1374864402','0','','4005362001201609224600140554','14745133566959Fb4unQbY','oLKWvwjBX8iC3jdpScA0kY53HaG8','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),('2016-09-22 10:56:04','wxb12b0b70efada027','1374864402','0','','4005402001201609224599871500','147451295558745Lkp7igi','oLKWvwnrRBnJnIuBokb_yehEP-as','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),

0
kukudeku
kukudeku

引用来自“kukudeku”的评论

package org.test;

public class StitchString {
 private static final String TABLE_FIELDS = "OPR_DATE,ID,PHONE,FIELD4,FIELD5,FIELD6,FIELD7,FIELD8";
 private static final String TABLE_NAME = "YOU_TABLE_NAME";
 public static void main(String[] args) {
  StringBuffer  sb = new StringBuffer();
  sb.append("INSERT INTO "+TABLE_NAME+" ("+TABLE_FIELDS+") VALUES ");
  String getString = new StitchString().new StringClass().getString();
//  System.out.println("获取到的字符串:\n"+getString);
  //利用\n获取字符串数组
  String[] rowStrings = getString.split("\n");
  for(int i = 0;i<rowStrings.length;i++){
   //将`取消
   String rowString = rowStrings[i];
   rowString = rowString.replaceAll("`", "");
//   System.out.println("取消`之后得字符串"+rowString);
   //利用,分割字符串
   StringBuffer finalString = new StringBuffer();
   String[] strSplit = rowString.split(",");
   for (int j=0;j<strSplit.length;j++) {
    //给每个字段添加单引号
//    System.out.println("添加引号之前:"+strSplit[j]);
    strSplit[j] = "'" + strSplit[j] + "'";
//    System.out.println("添加引号之后:"+strSplit[j]);
    finalString.append(strSplit[j]);
    if(j != strSplit.length -1){
     finalString.append(",");
    }
   }
   sb.append("(");
   sb.append(finalString);
   sb.append(")");
   if(i != rowString.length() - 1){
    sb.append(",");
   }
  }
  String sql_insert = sb.toString();
  System.out.println("插入语句"+sql_insert);
 }
 public interface StringIntf {
  public String getString();
 }
 public class StringClass implements StringIntf{

  @Override
  public String getString() {
   // TODO Auto-generated method stub
   StringBuffer sb = new StringBuffer();
   sb.append("`2016-09-22 11:09:23,`wxb12b0b70efada027,`1374864402,`0,`,`4001312001201609224600928240,`14745137559311W0RKs6Zb,`oLKWvwnbqnnFG0D2MuR3XIzJwuBs,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 11:05:37,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224600765713,`14745135295846tZQvrL96,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:45:46,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624248784,`14745303372997XwWKUTmB,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:38:35,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624469066,`14745299049539zH7CIqYR,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:48:56,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224624837680,`147453052657855c2e22dr,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 15:58:58,`wxb12b0b70efada027,`1374864402,`0,`,`4003722001201609224625173896,`14745311296774lQZoMr9H,`oLKWvwjx8jfjywI2VQHEv8CHpT0I,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`申请补、换领机动车行驶证,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 11:03:29,`wxb12b0b70efada027,`1374864402,`0,`,`4005362001201609224600140554,`14745133566959Fb4unQbY,`oLKWvwjBX8iC3jdpScA0kY53HaG8,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   sb.append("`2016-09-22 10:56:04,`wxb12b0b70efada027,`1374864402,`0,`,`4005402001201609224599871500,`147451295558745Lkp7igi,`oLKWvwnrRBnJnIuBokb_yehEP-as,`JSAPI,`SUCCESS,`CFT,`CNY,`0.01,`0.00,`0,`0,`0.00,`0.00,`,`,`测试一下,`,`0.00,`0.10%~\n");
   System.out.println("要返回的字符串:\n"+sb.toString());
   return sb.toString();
  }
 }
}

运行结果:

插入语句INSERT INTO YOU_TABLE_NAME (OPR_DATE,ID,PHONE,FIELD4,FIELD5,FIELD6,FIELD7,FIELD8) VALUES ('2016-09-22 11:09:23','wxb12b0b70efada027','1374864402','0','','4001312001201609224600928240','14745137559311W0RKs6Zb','oLKWvwnbqnnFG0D2MuR3XIzJwuBs','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),('2016-09-22 11:05:37','wxb12b0b70efada027','1374864402','0','','4003722001201609224600765713','14745135295846tZQvrL96','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),('2016-09-22 15:45:46','wxb12b0b70efada027','1374864402','0','','4003722001201609224624248784','14745303372997XwWKUTmB','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 15:38:35','wxb12b0b70efada027','1374864402','0','','4003722001201609224624469066','14745299049539zH7CIqYR','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 15:48:56','wxb12b0b70efada027','1374864402','0','','4003722001201609224624837680','147453052657855c2e22dr','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 15:58:58','wxb12b0b70efada027','1374864402','0','','4003722001201609224625173896','14745311296774lQZoMr9H','oLKWvwjx8jfjywI2VQHEv8CHpT0I','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','申请补、换领机动车行驶证','','0.00','0.10%~'),('2016-09-22 11:03:29','wxb12b0b70efada027','1374864402','0','','4005362001201609224600140554','14745133566959Fb4unQbY','oLKWvwjBX8iC3jdpScA0kY53HaG8','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),('2016-09-22 10:56:04','wxb12b0b70efada027','1374864402','0','','4005402001201609224599871500','147451295558745Lkp7igi','oLKWvwnrRBnJnIuBokb_yehEP-as','JSAPI','SUCCESS','CFT','CNY','0.01','0.00','0','0','0.00','0.00','','','测试一下','','0.00','0.10%~'),

最后的插入语句应该是你想要的sql语句吧

554330833a
554330833a
@kukudeku 回复@kukudeku : 恩,我批量的insert一般都是batch的,但是如果说connect多次取会影响性能的话我觉得有线程池不需要这样,我猜数据库内部既然有这种写法肯定有内部的优化,所以还是batch好
kukudeku
kukudeku
@554330833a 回复@554330833a : 唉……我说不明白。但是我知道,在操作同一个表的时候,要尽可能的少获取connection。能一次完成的就一次完成。以减少数据库压力。如果你觉得你的系统能够承受更多次的getConnection,这样做也是可以的。
554330833a
554330833a
@kukudeku 回复@kukudeku : 但是一般有会用连接池的吧,少并发应该可以,如果高并发估计就不行了
kukudeku
kukudeku
回复 @554330833a : batch执行一次只获取和释放一次连接,循环insert,每insert一次就要获取一次连接,执行完毕,commit然后close,然后再次获取一个连接。这很容易造成业务处理时间超时。如果业务量比较大,频繁获取connection容易造成数据库连接不够甚至会影响数据库性能
554330833a
554330833a
@kukudeku 回复@kukudeku : batch和for循环insert有什么区别?
下一页
0
kukudeku
kukudeku

引用来自“szwx855”的评论

进UE,选择列模式,在这些数据前面加上INSERT INTO TABLE ('ID','NAME','AGE') VALUES (你的数据)

就好了。

你的是MYSQL.可以通过IDE直接导出INSERT的,不用这样搞。

引用来自“szwx855”的评论

package com.linjian.TetMatch;

import org.apache.commons.lang.StringUtils;

/**
 * 〈一句话功能简述〉<br>
 * 〈功能详细描述〉
 *
 * @author 16080951(林健)
 * @Copyright (c) 2016 XXX技术有限公司 版权所有
 * @see [相关类/方法](可选)
 * @since [产品/模块版本] (可选)
 */
public class TestMatch {


    public static String genInsert(String cols[],String data[]) {

        StringBuilder sb = new StringBuilder("INSERT INTO TABLE ");

        sb.append("(");
        sb.append(StringUtils.join(cols, ","));
        sb.append(")");
        sb.append(" VALUES ");
        sb.append("(");
        for (String dataInfo : data) {
            sb.append("'");
            sb.append(dataInfo);
            sb.append("',");
        }
        sb = new StringBuilder(sb.substring(0, sb.length() - 1));
        sb.append(")");
        return sb.toString();
    }

    public static void main(String[] args) {

        String a = "`2016-09-22 11:09:23,`wxb12b0b70efada027,`1374864402,`0.00,`0.10%~";
        String data[] = a.replaceAll("`", "").split(",");
        String colsName[] = { "time","sn","mobile","value","pence" };
        String result = genInsert(colsName, data);
        System.out.println(result);


    }
}


========================结果===========================
INSERT INTO TABLE (time,sn,mobile,value,pence) VALUES ('2016-09-22 11:09:23','wxb12b0b70efada027','1374864402','0.00','0.10%~')

Process finished with exit code 0



你的方法更简洁,学习了
返回顶部
顶部