String sql ="select abc ,def from table1,table2";
sql=sql+" where abc=1"
sql=sql+" order by abc,def";

解决方案 »

  1.   

    String sql ="select abc ,def from table1,table2";
    sql=sql+" where abc=1"
    sql=sql+" order by abc,def";
      

  2.   

    String sql="select abc ,def from table1,table2 order by abc,def";
    String insert="where abc=1";
    StringBuffer sb=new StringBuffer(sql);
    sql=sb.insert(sql.indexOf("order"),insert+" ").toString();
      

  3.   

    楼主都说了。不仅仅是order by
    也有可能是having等情况。
      

  4.   

    上边的都没懂我的意思
    select rowid from pft,ss order by name
    改成:
    select rowid from pft,ss where rowid>=10 order by name 
    也可能是
    select rowid from pft group by name
    select rowid from pft where rowid>=10 group by name
    就是一个sql语句的变化情况,要求在其中加上限制条件where.
    大家懂了么?
    from后边的表可能是1......到n
    order可以换成任何可以在sql语句加上的限制条件的关键字.请看懂题再回答,谢谢
    不是简单的字符串加. 
      

  5.   

    没人会了么?
    就是找到任意一个sql语句中的表名结束的位置.
    这回可以懂了么?
      

  6.   

    String sqlA = "select a ,b, from " + table;
    String sqlB = " where 1=1 and " + codition;
      

  7.   

    我终于看懂楼主的意思了,那你告诉我这条SQL是你自己写呢?还有由用户输入还是什么其他途径得到的。因为从你的语句里找到表的名字不容易,中间的空格是不可预知的。
      

  8.   

    重新组织sql就可以了,没必要这么麻烦吧。
    分三段:
    String selectsql ="select * from tb1,tb2 ";
    String where="where **=** and **>** ";
    String other="";
      

  9.   

    public static String insertString(String srcString,String whereString){
            
            String rtnString = srcString.toUpperCase().trim();
            int insertIndex = -1;
            for(int i=0;i<4&&insertIndex==-1;i++){
                switch(i){
                    case 0:
                        insertIndex=rtnString.indexOf("WHERE");
                        if(insertIndex>0){
                            insertIndex = -1;
                            whereString = " And " + whereString.substring(5);
                        }
                        break;
                    case 1:
                        insertIndex=rtnString.indexOf("GROUP BY");
                        break;
                    case 2:
                        insertIndex=rtnString.indexOf("HAVING");
                        break;
                    case 3:
                        insertIndex=rtnString.indexOf("ORDER BY");
                        break;
                }
            }
            
            StringBuffer sb=new StringBuffer(srcString);
            rtnString = sb.insert(insertIndex,whereString + " ").toString();
            return rtnString;
        }
      

  10.   

    只是限制一个结果集可以用临时表,select t.* from (select .....) t where t.a=111
      

  11.   

    重新组织sql就可以了,没必要这么麻烦吧。
    分三段:
    String selectsql ="select * from tb1,tb2 ";
    String where="where **=** and **>** ";
    String other="";你好好看看问题就知道你得方法不行了.
      

  12.   

    重新组织sql就可以了,没必要这么麻烦吧。
    分三段:
    String selectsql ="select * from tb1,tb2 ";
    String where="where **=** and **>** ";
    String other="";你好好看看问题就知道你得方法不行了.
      

  13.   

    public static String insertString(String srcString,String whereString){
            
            String rtnString = srcString.toUpperCase().trim();
            int insertIndex = -1;
            for(int i=0;i<4&&insertIndex==-1;i++){
                switch(i){
                    case 0:
                        insertIndex=rtnString.indexOf("WHERE");
                        if(insertIndex>0){
                            insertIndex = -1;
                            whereString = " And " + whereString.substring(5);
                        }
                        break;
                    case 1:
                        insertIndex=rtnString.indexOf("GROUP BY");
                        break;
                    case 2:
                        insertIndex=rtnString.indexOf("HAVING");
                        break;
                    case 3:
                        insertIndex=rtnString.indexOf("ORDER BY");
                        break;
                }
            }
            
            StringBuffer sb=new StringBuffer(srcString);
            rtnString = sb.insert(insertIndex,whereString + " ").toString();
            return rtnString;
        }
      

  14.   

    to:sakura8sakura(ouyangrui) 
    你得方法很好,我也想过,但是有没有想过要是有其他得关键字,这个程序是不是就是一个错得程序了
      

  15.   

    select语句除了SELECT、FROM、WHERE、GROUP BY、HAVING、ORDER BY外,就是一些函数关键字,还能有什么关键字?
      

  16.   

    SELECT和FROM是在WHERE前面出现的,可以不用考虑。
      

  17.   

    static String insert(String sql,String where){
    int i=getPos(sql);
    return sql.substring(0,i)+where+" "+sql.substring(i);
    }
    static int ignoreSpace(String sql,int begin){
    int i=begin;
    while(Character.isWhitespace(sql.charAt(i))){
    i++;
    }
    return i;
    }
    static int getPos(String sql){
    int end=0;
    String temp=sql.toLowerCase();
    int iFrom=temp.indexOf("from");
    end=iFrom+4;
    boolean isEnd=false;

    end=ignoreSpace(sql,end);

    while((end<sql.length())){
    char c=sql.charAt(end);

    if(Character.isWhitespace(c)){
    end=ignoreSpace(sql,end);
    c=sql.charAt(end);
    if(c!=','){
    return end;
    }
    else{
    continue;
    }
    }
    else{
    end++;
    }
    }
    return end;

    }
      

  18.   

    改进一下
    static int getPos(String sql){
    int end=0;
    String temp=sql.toLowerCase();
    int iFrom=temp.indexOf("from");
    end=iFrom+4;
    boolean isEnd=false;

    end=ignoreSpace(sql,end);

    while((end<sql.length())){
    char c=sql.charAt(end);

    if(Character.isWhitespace(c)){
    end=ignoreSpace(sql,end);
    c=sql.charAt(end);
    if(c!=','){
    return end;
    }
    else{  
                                                  end=ignoreSpace(sql,end+1);//OK!
    continue;
    }
    }
    else{
    end++;
    }
    }
    return end;

    }