String temp[]={};
temp[i]=st.nextToken();
就是这里出错了。因为你的temp虽然初始化了,但是长度是0。就是只给了一个指针,但是空间是0。
建议你用
ArrayList temp = new ArrayList();
temp.add(st.nextToken());
temp[i]=st.nextToken();
就是这里出错了。因为你的temp虽然初始化了,但是长度是0。就是只给了一个指针,但是空间是0。
建议你用
ArrayList temp = new ArrayList();
temp.add(st.nextToken());
或者你也可以这样
String temp[st.countTokens]
import java.io.*;
public class sqlTrans {
private String FINAL_SQL;
public sqlTrans() {
FINAL_SQL = new String();
}
public String trans(String sql,String fore_add){
StringTokenizer st=new StringTokenizer(sql);//对sql语句进行分解,便于提取,操作
String temp[]=new String[st.countTokens()];
//获得字段个数,并赋值给中间变量数组temp
int i = 0;
while(st.hasMoreTokens()){
temp[i++]=st.nextToken();
}
for(i=0;i<temp.length;i++){
if(temp[i].equalsIgnoreCase("from")){
temp[i+1]=fore_add.concat(temp[i+1]);//字符串操作,加上前缀按设计要求输出
}
}
for(i=0;i<temp.length;i++){//生成新的sql字符串
FINAL_SQL=FINAL_SQL+" "+temp[i];
}
return FINAL_SQL; }
public static void main(String[] args) {
String tsql="";
String sq="select * from banny where id=45";
String fd="df_";
sqlTrans sqlt = new sqlTrans();
tsql=sqlt.trans(sq,fd);
System.out.println(tsql);
}}
是想得到这样的结果吧?