如果使用Statement 从理论上说,认证网页中会有型如: select * from admin where username='XXX' and password='YYY' 的语句,若在正式运行此句之前,没有进行必要的字符过滤,则很容易实施SQL注入。 如在用户名文本框内输入:abc' or 1=1-- 在密码框内输入:123 则SQL语句变成: select * from admin where username='abc' or 1=1 and password='123' 不管用户输入任何用户名与密码,此语句永远都能正确执行,用户轻易骗过系统,获取合法身份。但是使用PreparedStatement时: select * from admin where username=? and password=? 被替换成 select * from admin where username="abc' or 1=1" and password=null 就防止了注入式攻击,即字符串整体作为参数进行匹配
从理论上说,认证网页中会有型如:
select * from admin where username='XXX' and password='YYY' 的语句,若在正式运行此句之前,没有进行必要的字符过滤,则很容易实施SQL注入。
如在用户名文本框内输入:abc' or 1=1-- 在密码框内输入:123 则SQL语句变成:
select * from admin where username='abc' or 1=1 and password='123' 不管用户输入任何用户名与密码,此语句永远都能正确执行,用户轻易骗过系统,获取合法身份。但是使用PreparedStatement时:
select * from admin where username=? and password=?
被替换成
select * from admin where username="abc' or 1=1" and password=null
就防止了注入式攻击,即字符串整体作为参数进行匹配