日期:2014-05-20 浏览次数:20960 次
import java.io.IOException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class test {
static int i1 = 0;
public static String GetTableName(String SQL,String regex){
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(SQL);
StringBuffer sb = new StringBuffer();
while (matcher.find()) {
System.out.println(SQL);
System.out.println("rod: (" + matcher.start() + ", " + matcher.end() + ")");
System.out.println("sub: " + SQL.substring(matcher.start(), matcher.end()));
matcher.appendReplacement(sb, "");
}
matcher.appendTail(sb);
return null;
}
public static void main(String[] args) throws IOException{
String SQL1="create \n table \n \r t1 (tc1 int primary key, tc2 int) enable primary key using index";
String regex_ct="create(\\s*)table(\\s*)(\\w*)(\\s*)\\(";
GetTableName(SQL1,regex_ct);
}
}
String sql = "create \n table \n \r t1 (tc1 int primary key, tc2 int) enable primary key using index";
int index = sql.toLowerCase().indexOf("table");
if (index != -1) {
String temp = sql.substring(index + 5).trim();
index = temp.indexOf(" ");
if (index != -1) {
System.out.println("表名:"+temp.substring(0, index));
} else {
System.out.println("错误的SQL");
}
} else {
System.out.println("错误的SQL");
}