SQL注入问题和PreparedStatement
在实际的开发中我们并不建议使用Statement接口,而是使用PreparedStatement。
Statement存在的问题:
因为Statement是执行一条完整SQL语句,所以就存在一定的问题
- SQL拼接
- 当我们写一条很多条件的查询语句时,或者insert添加一条数据时,我们要用很多+来拼接出一个完整的Sql语句,拼接的越长可读性也就越差
- SQL注入
- Statement直接执行一条拼接好的SQL语句,错误的拼接也就会影响到SQL语句的执行
- SQL注入就是利用了一些系统没有对用户输入的数据进行充分的检查,而用户输入数据中注入一些非法的SQL语句段或命令,从而利用系统的SQL引擎完成恶意行为的做法,为了防止这个情况的发生,我们也要PreparedStatement取代Statement
- 无法处理Blob类型的数据
- BLOB:二级制大对象,BLOB常常是数据库用来存储二进制未见的字段类型,我们无法使用字符串拼接一个二级制数据,所以这时候我们也需要使用PreparedStatement
SQL注入
会发生SQL注入的案例:
//尝试使用Statement模拟用户登陆
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("输入用户名:");
String name = sc.nextLine();
System.out.println("输入密码:");
String password = sc.next();
login(name,password);
}
public static void login(String name,String password){
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String sql = "select * from user where name='"+name+"'and password='"+password+"'";
try {
connection = DbUtils2.getConnection();
statement = DbUtils2.getStatement(connection);
resultSet = statement.executeQuery(sql);
if (resultSet!=null){
System.out.println("********************登录成功********************");
}else{
System.out.println("********************登录失败********************");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally{
DbUtils2.close(connection,statement,resultSet);
}
}
运行程序,我们并不知道到用户和密码的情况,如果这样输入,仍然可以实现登陆
(-- '之间一定要有空格)
这种写法就相当于:
select * from user where name='xxx' and password='x ' or true -- '
where ··· or true这个条件一定成立,(-- )把后面的SQL语句屏蔽掉
PreparedStatement接口
PreparedStatement是Statement的子接口,继承了Statement接口中的所有方法,它是一个预编译的SQL语句
PreparedStatement执行原理:
- Statement对象执行一条SQL语句都会先得到SQL语句发送给数据库,数据库再进行编译和执行。如果有一万条SQL语句,数据库需要编译一万次,执行一万次,效率低。
- PreparedStatement会先将SQL语句发送给数据库预编译,PreparedStatement会引用预编译后的结果,我们可以多次传入不同的参数给PreparedStatement对象执行,减少了SQL语句的编译时间,提高了效率
DBServer(数据库管理工具)会对预编译语句提供性能优化。那么预编译语句有可能被重复调用,所以语句被DBServer的编译器编译后的执行代码被缓存下来,那么下次调用时只要是相同的预编译语句不就需要编译,只要将参数直接传入编译过的语句执行代码中,就会得到执行。
使用PreparedStatement的好处:
- 效率高,可以写参数化查询,可以进行预处理,可以写动态的查询语句
- 安全性好,防止SQL注入
- 提高了程序的可读性,SQL中使用?占位符,执行只需要将值传入即可
- 可以处理Blob类型的数据
创建PreparedStatement对象
- 使用Connection中的方法:
- PreparedStatement prepareStatement==(String sql)==;使用指定的SQL语句,返回一个预编译的SQL语句对象
PreparedStatement中的方法
PreparedStatement用法和Statement差不多,不同的是,创建PreparedStatement对象时,需要将sql语句传入作为预编译,调用executeQuery()和executeUpdate()之前,要给SQL语句中的?占位符赋值,作为一个完整的语句执行。
- 占位符赋值,需要使用setXxx(int parameterIndex,Xxx x)方法;为指定的占位符位置赋值为x。
- (Xxx为类型,如果传入的是一个int类型的数据,那么使用setInt(1,100),为第一个占位符赋值为100;占位符的索引位置是从一开始的,有几个?就要set几个)
示例:
public static void login2(String name,String password){
Connection connection = null;
PreparedStatement ps = null;
ResultSet resultSet = null;
String sql = "select * from user where name= ? and password= ? ";
try {
connection = DbUtils2.getConnection();
ps = connection.prepareStatement(sql);
//将相应的值传入?占位符位置,占位符的索引是从1开始的!
ps.setString(1,name);
ps.setString(2,password);
//执行查询语句
resultSet = ps.executeQuery();
if (resultSet!=null){
System.out.println("********************登录成功********************");
}else{
System.out.println("********************登录失败********************");
}
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally{
DbUtils2.close(connection,ps,resultSet);
}
}
(实际开发中其实都是PreparedStatement!后面使用连接池和DbUtils后这些步骤也就不需要我们自己完成了)