为什么Statement会被sql注入
因为Statement之所以会被sql注入是因为SQL语句结构发生了变化。比如:
"select*from tablename where username='"+uesrname+
"'and password='"+password+"'"
在用户输入’or true or’之后sql语句结构改变。
select*from tablename where username=''or true or'' and password=''
这样本来是判断用户名和密码都匹配时才会计数,但是经过改变后变成了或的逻辑关系,不管用户名和密码是否匹配该式的返回值永远为true;
为什么Preparement可以防止SQL注入。
因为Preparement样式为
select*from tablename where username=? and password=?
该SQL语句会在得到用户的输入之前先用数据库进行预编译,这样的话不管用户输入什么用户名和密码的判断始终都是并的逻辑关系,防止了SQL注入
本文详细解释了SQL注入攻击的工作原理及其对数据库安全性的威胁。通过对比Statement与Preparement两种方式,阐述了如何有效防止SQL注入,保障数据安全。

被折叠的 条评论
为什么被折叠?



