主要解析delete ,insert,update语句。将之解析为字段的含义。insert into a40(sortid,a4001,a4003,a4005_1,a4005_2,a4007,a4009,id,leader_code) values ('1','1','2010-05-11','部门一','500000000000','01','5000','1','2130000903')
insert into a40(sortid,a4001,a4003,a4005_1,a4005_2,a4007,a4009,id,leader_code) values ('1','1','2010-05-11','部门一','500000000000','01','5000','1','2130000239')
insert into a40(sortid,a4001,a4003,a4005_1,a4005_2,a4007,a4009,id,leader_code) values ('1','1','2010-05-11','部门一','500000000000','01','5000','1','2130000039')如何将多条sql语句解析为具体的某个字段所作的操作,主要是看列名及其变动值,比如说。上面的解析为 字段=某个值排序号=1,序号=1,时间='2010-05-11',所作部门=部门一 等等insert into table(item1,item2,item3) values(111,222,333)将这个解析为item1=111,item2=222,item3=333
如果是删除 的话是,delete from a01 where name='abc' and sex='2' 解析为 姓名='abc',性别='2'
当然实际中还包括更加复杂的
修改语句
update a01 set name='abc',sex='2' where name='ttt'解析为 姓名='abc',性别='2' 考虑到用正则表达式。但是在很多地方不好实现。所以征求下,有没有好的方案。
insert into a40(sortid,a4001,a4003,a4005_1,a4005_2,a4007,a4009,id,leader_code) values ('1','1','2010-05-11','部门一','500000000000','01','5000','1','2130000239')
insert into a40(sortid,a4001,a4003,a4005_1,a4005_2,a4007,a4009,id,leader_code) values ('1','1','2010-05-11','部门一','500000000000','01','5000','1','2130000039')如何将多条sql语句解析为具体的某个字段所作的操作,主要是看列名及其变动值,比如说。上面的解析为 字段=某个值排序号=1,序号=1,时间='2010-05-11',所作部门=部门一 等等insert into table(item1,item2,item3) values(111,222,333)将这个解析为item1=111,item2=222,item3=333
如果是删除 的话是,delete from a01 where name='abc' and sex='2' 解析为 姓名='abc',性别='2'
当然实际中还包括更加复杂的
修改语句
update a01 set name='abc',sex='2' where name='ttt'解析为 姓名='abc',性别='2' 考虑到用正则表达式。但是在很多地方不好实现。所以征求下,有没有好的方案。
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Test {
public static void main(String[] args) {
String[] array = {
"insert into table(item1,item2,item3) values(111,222,333)",
"delete from a01 where name='abc' and sex='2'",
"update a01 set name='abc',sex='2' where name='ttt'" };
Map<String, String> insertMap = new HashMap<String, String>();
Map<String, String> deleteMap = new HashMap<String, String>();
Map<String, String> updateMap = new HashMap<String, String>();
Pattern p = null;
Matcher m = null;
for (String str : array) {
if (str.startsWith("insert")) {
p = Pattern.compile("\\(([^()]*)\\)");
m = p.matcher(str);
String[] key = null;
int pos = 0;
if (m.find()) {
key = m.group(1).split(",");
pos = m.end();
}
String[] values = null;
if (m.find(pos)) {
values = m.group(1).split(",");
if (key.length == values.length) {
for (int i = 0; i < key.length; i++)
insertMap.put(key[i], values[i]);
}
}
} else if (str.startsWith("delete")) {
p = Pattern.compile("\\w+\\s*=\\s*'[^'']+'");
m = p.matcher(str);
while (m.find()) {
String[] arraykey = m.group().split("=");
deleteMap.put(arraykey[0].trim(), arraykey[1].trim());
}
} else {
p = Pattern.compile("\\w+\\s*=\\s*'[^'']+'");
m = p.matcher(str);
while (m.find()) {
String[] arraykey = m.group().split("=");
if (updateMap.isEmpty()
|| !updateMap.containsKey(arraykey[0].trim()))
updateMap.put(arraykey[0].trim(), arraykey[1].trim());
}
} }
System.out.println(insertMap);
System.out.println(deleteMap);
System.out.println(updateMap);
}
}
你的代码就死翘翘了!