推荐能使用#就不要使用 $
a.#{ }
1.相当于JDBC中的PreparedStatement ,是经过预编译的,安全
2.会为参数自动拼接引号
3.执行sql效果
select * from user where uid="1" and username="cathy"
b.${ }
1.相当于JDBC中的Statement ,未经过预编译,非安全,存在sql注入危险
2.并不会给参数自动拼接引号
3.执行sql效果
select * from user where uid=1 and username=cathy
写一个sql注入的例子
假设该表中有1个用户,账号为cathy,密码为:pwd123
理想查询条件应为
select * from user where username=cathy and password=pwd123
后台sql代码
String sql = "SELECT * " +
"FROM user "+
"WHERE username='"+userName+"' " +
"AND password='"+password+"'";
但是如果用户恶意输入:
用户名:随便输
密码 :’ OR ‘1’='1
那么实际查询的sql为
select * from user where username='lalalala' and password='' or '1'='1'
能显示登录,此则为sql注入攻击