public void insertSQL(String tabName,Map<String, String> map) {
String key = "";
String values = "";
Iterator entries = map.entrySet().iterator();
for (String k : map.keySet()) {
if (!"".equals(k)) {
key = key + k + ",";
}
else{
key = key.substring(0,key.length()-1);
}
}
for (String v : map.values()) {
if (!"".equals(v)) {
values = values + v + ",";
}
else{
values = values.substring(0,values.length()-1);
}
}
System.out.println(key);
System.out.println(values);
String sql = "insert into "+tabName+"("+ key +") values ("+ values +")";
System.out.println(sql);
}
结果:
name,age,
张三,值1,
insert into 学生(name,age,) values (张三,值1,)
看了半天也不懂为什么substring方法失效了