有一字符串"select * from test where id=1"我想把"test"和"where id=1"分出来,放到两个参数里面,要怎么分啊?

解决方案 »

  1.   

    一个是表名
    一个是where条件根据from和where两个关键字split或者substring都可以的
      

  2.   

    我是这样理解的:
    "select * from " + tableName + condition   其中tableName参数对应test,condition参数对应where id=1
      

  3.   


    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);
    }
      

  4.   

    sql.split(" from ")[1].split(" ", 2);
    能不能解释一下啊?
      

  5.   


    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));
    }
    }
      

  6.   

    简单的sql容易处理,就是不知道有没有复杂的sql,包含子查询之类的
      

  7.   

    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"]
    希望对你有帮助。
      

  8.   

    String str="select * from test where id=1";
    String []array=str.split(" ");
    String str1=array[3];
    String str2=array[4];
      

  9.   

    我的理解是你要把表名和查询条件分出来。一种就是拼字符串,另一种如果是在JDBC,直接用preparedStatement.
      

  10.   

    StringTokenizer strToken = new StringTokenizer("select * from test where id=1"," ");