Recent business needs to change a table to an update statement because the only primary key relationship is set and the direct insert will be abnormal, so I changed all insert statements to update statements.
1. this is the sql file exported from a colleague's computer.
prompt Importing table gams_jc_assetclass... set feedback off set define off insert into Table name (ID, CARDDEFINE, CARDDEFINENAME) values ('3755B565400004411CD54EF60E8348D5', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'); insert into Table name (ID, CARDDEFINE, CARDDEFINENAME) values ('3755B56540000461322D77FA1CC14083', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'); insert into Table name (ID, CARDDEFINE, CARDDEFINENAME) values ('3755B56540000481951785168353D811', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'); insert into Table name (ID, CARDDEFINE, CARDDEFINENAME) values ('3755B565400004A1D9F41574552BC272', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'); insert into Table name (ID, CARDDEFINE, CARDDEFINENAME) values ('3755B565400004C125E5C8BD187FBE44', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'); insert into Table name (ID, CARDDEFINE, CARDDEFINENAME) values ('3755B565400004E1C875EC1C1B0FA87E', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'); prompt Done.
2. Create a new sql file and change sql to the following format:
(Delete the header and tail, replace the parentheses of each data with empty, insertinto - table name with empty, and place each statement in one line by substitution)
ID, CARDDEFINE, CARDDEFINENAME values '3755B565400004411CD54EF60E8348D5', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB' ID, CARDDEFINE, CARDDEFINENAME values '3755B56540000461322D77FA1CC14083', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB' ID, CARDDEFINE, CARDDEFINENAME values '3755B56540000481951785168353D811', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB' ID, CARDDEFINE, CARDDEFINENAME values '3755B565400004A1D9F41574552BC272', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB' ID, CARDDEFINE, CARDDEFINENAME values '3755B565400004C125E5C8BD187FBE44', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB' ID, CARDDEFINE, CARDDEFINENAME values '3755B565400004E1C875EC1C1B0FA87E', '100F4A9BE5D2F5BEFBB7C8FA0883835F', 'GAMS_CARD_TYSB'
3. Then there's the code:
It's a bit lazy. It's output directly. Just write an output stream to the last list of main methods that output directly to files.
package com.jiuqi.io; import java.io.BufferedReader; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; //Read sql file //Separate strings with ";" as a list //Change list < String > to List < Map > separated by value //Stitching strings into list s, for example //Printout //There were more than 3000 pieces. Now it has transformed into more than 500 items, all of which are three and one repetition. public class test { private static final String star = "update Table name set"; private static final String end = ";"; private static final String CARDDEFINE = " CARDDEFINE"; private static final String CARDDEFINENAME = " CARDDEFINENAME"; private static final String ID = "ID"; //private static List<String> endList = new ArrayList<>(); public static void main(String[] args) throws IOException { String path = "D:\\sql.txt"; List<String> list = readSqlFile(path); List<Map<String,String>> listMap = spilStr(list); /*for (Map<String, String> map : listMap) { System.out.println(map); }*/ List<String> sqlList = getStrSql(listMap); /*for (Map<String,String> item : listMap) { System.out.println(item.get(ID)); }*/ } //read file public static List<String> readSqlFile(String path) throws IOException { FileReader file = new FileReader(path); BufferedReader br = new BufferedReader(file) ;//Convert file to //Netizens recommend more concise writing List<String> list = new ArrayList<>(); String line ; while ((line = br.readLine()) != null) { list.add(line); // Read in a row at a time //System.out.println(line); } return list; } //Separate arrays with value and store them in list < Map > public static List<Map<String,String>> spilStr(List<String> list) { String[] a ; String[] mapKey; String[] mapValue; List<Map<String,String>> listMap = new ArrayList<>(); for (String item : list) { Map<String,String> map = new HashMap<>(); if(!item.equals("") && null!=item) { a = item.split(" values ");//The first three key s in the array and the last three corresponding value s mapKey = a[0].split(","); mapValue = a[1].split(","); for(int i =0 ; i < mapKey.length ; i ++) { map.put(mapKey[i], mapValue[i]); } listMap.add(map); } } return listMap; } /*private static final String star = "update gams_jc_assetclass set"; private static final String end = ";\n"; private static final String CARDDEFINE = " CARDDEFINE"; private static final String CARDDEFINENAME = " CARDDEFINENAME"; private static final String ID = "ID"; */ //Assemble sql public static List<String> getStrSql(List<Map<String,String>> listMap){ List<String> sqlList = new ArrayList<>(); String sql ; for (Map<String,String> item : listMap) { sql = star + CARDDEFINE + "=" + item.get(CARDDEFINE) + "," + CARDDEFINENAME +"="+item.get(CARDDEFINENAME) +" "+ "where " + ID +"="+item.get(ID) + end ; sqlList.add(sql); } for (String sqlItem : sqlList) { System.out.println(sqlItem); } return sqlList; } }