加载中...

JDBC-防止SQL注入问题

 

String sql = "select * from user where name = '" + name + "' and password = '" + password + "' ;";

//说明:使用拼接的sql语句会有sql注入问题

System.out.println(sql);

......省略操作数据库代码............

 

控制台操作

请输入用户名:

123

请输入密码:

123' or 1 ='1

 

select * from user where name = '123' and password = '123' or 1 = '1' ;

登录成功...

 

解决方法:

preparedStatement:预编译对象, 是Statement对象的子类。

特点:

    性能要高

    会把sql语句先编译,使用?号作为sql参数的占位符,再通过方法添加sql参数解决SQL注入问题

posted @ 2018-12-18 11:44  royal6  阅读(695)  评论(0编辑  收藏  举报