您的位置:首页 > 数据库

防止sql注入等的危险

2016-04-05 00:00 459 查看
摘要: 防止sql注入,使用?占位符并结合 PreparedStatement 并设置参数,避免使用串联字符串

@Test
public void testLogin2() throws Exception {
Connection con = ConnUtils.getCon();

Scanner sc = new Scanner(System.in);
String name = sc.nextLine();
String pwd = sc.nextLine();
//使用?占位符,不能用串联字符串
String sql ="select * from users where name=? and pwd=?";
//执行sql语句使用

PreparedStatement pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, pwd);
System.err.println(sql);
ResultSet rs = pst.executeQuery();
if(rs.next()){
System.err.println("登录成功");
}else{
System.err.println("用户名或是密码错误..");
}

pst.close();
con.close();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: