String sql ="select abc ,def from table1,table2"; sql=sql+" where abc=1" sql=sql+" order by abc,def";
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();
楼主都说了。不仅仅是order by 也有可能是having等情况。
上边的都没懂我的意思 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语句加上的限制条件的关键字.请看懂题再回答,谢谢 不是简单的字符串加.
没人会了么? 就是找到任意一个sql语句中的表名结束的位置. 这回可以懂了么?
String sqlA = "select a ,b, from " + table; String sqlB = " where 1=1 and " + codition;
sql=sql+" where abc=1"
sql=sql+" order by abc,def";
String insert="where abc=1";
StringBuffer sb=new StringBuffer(sql);
sql=sb.insert(sql.indexOf("order"),insert+" ").toString();
也有可能是having等情况。
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语句加上的限制条件的关键字.请看懂题再回答,谢谢
不是简单的字符串加.
就是找到任意一个sql语句中的表名结束的位置.
这回可以懂了么?
String sqlB = " where 1=1 and " + codition;
分三段:
String selectsql ="select * from tb1,tb2 ";
String where="where **=** and **>** ";
String other="";
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;
}
分三段:
String selectsql ="select * from tb1,tb2 ";
String where="where **=** and **>** ";
String other="";你好好看看问题就知道你得方法不行了.
分三段:
String selectsql ="select * from tb1,tb2 ";
String where="where **=** and **>** ";
String other="";你好好看看问题就知道你得方法不行了.
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;
}
你得方法很好,我也想过,但是有没有想过要是有其他得关键字,这个程序是不是就是一个错得程序了
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;
}
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;
}