sql注入攻击指的是通过构建特殊的输入作为参数传入web应用程序,而这些输入大都是sql语法里的一些组合,通过执行sql语句进而执行攻击者所要的操作,其主要原因是程序没有细致地过滤用户输入的数据,致使非法数据侵入。
preparestatement方法是防止sql注入的简单有效手段
preparedstatement和statement的区别
1、preparedstatement是statement的子方法
2、preparedstatement可以防止sql注入的问题
3、preparedstatement它可以对它所代表的sql语句进行预编译,以减轻服务器压力
实例如下:
public user find(string username, string password) {
connection conn = null;
preparedstatement st = null;
resultset rs = null;
try{
conn = jdbcutils.getconnection();
string sql = "select * from users where username=? and password=?";
st = conn.preparestatement(sql);
st.setstring(1, username);
st.setstring(2, password);
rs = st.executequery(); //
if(rs.next()){
user user = new user();
user.setid(rs.getstring("id"));
user.setusername(rs.getstring("username"));
user.setpassword(rs.getstring("password"));
user.setemail(rs.getstring("email"));
user.setbirthday(rs.getdate("birthday"));
return user;
}
return null;
}catch (exception e) {
throw new daoexception(e);
}finally{
jdbcutils.release(conn, st, rs);
}
}
像我这样叼的有七个