String sql = "select * from test where id = 1 and name = 77"; String[] s = sql.split(" from ")[1].split(" ", 2); for (String a : s) { System.out.println(a); }
sql.split(" from ")[1].split(" ", 2); 能不能解释一下啊?
public static void main(String[] args){ String s = "select * From test where id=1"; Matcher m = Pattern.compile("(?i)select\\s+.+?(?i)from\\s+(.+?)\\s+((?i)where\\s+.+)").matcher(s); while(m.find()){ System.out.println(m.group(1)); System.out.println(m.group(2)); } }
简单的sql容易处理,就是不知道有没有复杂的sql,包含子查询之类的
sql.split(" from ")表示将 String sql = "select * from test where id = 1 and name = 77"; 按照关键字" from "分割成字符串数组,这里可以清楚知道分成两个String["select *","test where id = 1 and name = 77"] 然后取sql.split(" from ")[1],也就是"test where id = 1 and name = 77",再按照关键字" "分割 split(" ", 2);其中2将返回的数组元素个数限制为两个 最后结果就得到了["test", "where id = 1 and name = 77"] 希望对你有帮助。
String str="select * from test where id=1"; String []array=str.split(" "); String str1=array[3]; String str2=array[4];
一个是where条件根据from和where两个关键字split或者substring都可以的
"select * from " + tableName + condition 其中tableName参数对应test,condition参数对应where id=1
String sql = "select * from test where id = 1 and name = 77";
String[] s = sql.split(" from ")[1].split(" ", 2);
for (String a : s) {
System.out.println(a);
}
能不能解释一下啊?
public static void main(String[] args){
String s = "select * From test where id=1";
Matcher m = Pattern.compile("(?i)select\\s+.+?(?i)from\\s+(.+?)\\s+((?i)where\\s+.+)").matcher(s);
while(m.find()){
System.out.println(m.group(1));
System.out.println(m.group(2));
}
}
String sql = "select * from test where id = 1 and name = 77";
按照关键字" from "分割成字符串数组,这里可以清楚知道分成两个String["select *","test where id = 1 and name = 77"]
然后取sql.split(" from ")[1],也就是"test where id = 1 and name = 77",再按照关键字" "分割
split(" ", 2);其中2将返回的数组元素个数限制为两个
最后结果就得到了["test", "where id = 1 and name = 77"]
希望对你有帮助。
String []array=str.split(" ");
String str1=array[3];
String str2=array[4];