标签:string sql connect query select span print system 改变
sql注入的前提是采用的动态拼接(字符串拼接)的的方式:
如下:"select * from user where userName=’” + userName + ”’ and password = ‘”+password”’
--------------------
张三 123456
select * from user where userName=‘张三‘ and password=‘123456‘
---------------------
‘张三‘; --
select * from user where userName=‘张三‘;--‘and password=‘123456‘
不需要密码也能登录了。
-------------
解决方案:使用preparedStatement的参数化sql,通过先确定语义,再传入参数,就不会因为传入的参数改变sql的语义。
(通过setInt,setString,setBoolean传入参数)
//建立数据连接
conn=ds.getConnection();
//1.设置prepareStatement带占位符的sql语句
PreparedStatement ptmt = conn.prepareStatement("select * from user where userName = ? and password = ?");
ptmt.setString(1, "张三"); //2.设置参数
ptmt.setString(2, "123456");
rs=ptmt.executeQuery();
while(rs.next()){
System.out.println("登陆成功");
return;
}
System.out.println("登陆失败");
关键点:占位符、数据类型控制
摘抄自:https://blog.csdn.net/qq_30258957/article/details/78145885
标签:string sql connect query select span print system 改变
原文地址:https://www.cnblogs.com/haiqin/p/9201277.html