日期:2014-05-17 浏览次数:20866 次
public static void main(String[] args) {
String reg="[update|drop|truncate|alter|set|delete|insert|exec]*";
String str="update xxx from fff";
String str2="drop xx from dd.";
Pattern p =Pattern.compile(reg);
//
Matcher m=p.matcher(str);
System.out.println(m.matches());
//
m=p.matcher(str2);
System.out.println(m.matches());
}
------解决方案--------------------
[^update]|[^drop]|[^truncate]|[^alter]|[^set]|[^delete]|[^insert]|[^exec]* 手写的没有试。。你试试吧
------解决方案--------------------
public static void main(String[] args) throws Exception{
String sql = "update set tab s = 'a'";
Matcher m = Pattern.compile("[update|drop|truncate|alter|set|delete|insert|exec]").matcher(sql);
System.out.println(!m.find());
}
------解决方案--------------------
或者:.*?[update|drop|truncate|alter|set|delete|insert|exec].*?
------解决方案--------------------
你的意思是不是在客户端验证(JAVASCRIPT的正则?)
------解决方案--------------------
javascript校验:
function checkSQL(sql){
var allowExt = "update|drop|truncate|alter|set|delete|insert|exec|";
if(allowExt != 0 && allowExt.indexOf(sql + "|") == -1){
alert("【sql语句中不能包含 update|drop|truncate|alter|set|delete|insert|exec】!");
return false;
}
}
------解决方案--------------------
用jdbc的preparedStatement参数处理,就不用担心关键字的问题啦。。