String expr = "select * from table where url like ?";
pstmt = con.prepareStatement(expr);
String a="a";
pstmt.setString(1, "%"+a+"%");
pstmt.execute();
这样写 PreparedStatement 会默认生成sql语句: select * from table where url like '%http%'
不要这样写
- String expr = "select * from table where url like %?%";
String expr = "select * from table where url like %?%";
也不要这样写
- pstmt = con.prepareStatement(expr);
- String a="%a%";
- pstmt.setString(1, a);
- pstmt.execute();
本文介绍如何正确使用PreparedStatement防止SQL注入攻击,展示了错误的参数拼接方式可能导致的安全问题,并给出了正确的示例。
853

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



