Statement基本介绍
1.Statement对象,用于执行静态SQL语句并返回其生成的结果的对象
2.在连接建立后,需要对数据库进行访问,执行命令或是SQL语句,可以通过
①Statement [现在不建议使用了,因为Statement对象执行SQL语句,存在SQL注入风险]
②PreparedStatement [叫:预处理,是Statement的子接口] (现在基本上都是用这个)
③CallableStatement [存储过程]
3.什么叫SQL注入:SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命令,恶意攻击数据库。
如:Select * from admin where name = ' 1 ' OR ' AND pwd ' OR '1' = '1'
4.要防范SQL注入,用PreparedStatement代替Statement就行
比如我下面的这个代码(预处理查询):
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生学号:");
String Sno=scanner.nextLine();
System.out.println("请输入学生姓名:");
String Sname=scanner.nextLine();
//连接数据库
Properties properties =new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));//用getProperty()获取相关的值
String url = properties.getProperty("url");
String user= properties.getProperty("user");
String password=properties.getProperty("password");
String driver = properties.getProperty("driver");
//1.注册驱动
Class.forName(driver);
//2.获得连接
Connection connection = DriverManager.getConnection(url,user,password);//网络连接
//3.组织sql,SQL的?就相当于占位符等着后面设置值
String sql="select Sname,Sno from s where Sno=? and Sname=?";
//4.获得Statement对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,Sno);//设置第一个问号那里的值
preparedStatement.setString(2,Sname);//第二个问号那里的值
//5.执行sql返回单个ResultSet对象
ResultSet resultSet = preparedStatement.executeQuery();//在这个executeQuery中不能再传sql回去了不然就传的是select Sname,Sno from s where Sno=? and Sname=?
//当然如果这里sql=select Sname,Sno from s where Sno='2010530223' and Sname=欧健
//并且没有32,33行时才可以传变量sql
//6.用while 循环取出数据
if (resultSet.next()){
System.out.println("登录成功");
}else{
System.out.println("登录失败");
}
//关闭
resultSet.close();
preparedStatement.close();
connection.close();
}
}
这个时候呢:再使用万能密码可就不行了,也就是是很有效的预防了SQL注入的这个问题
预处理DML:
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
/*
演示PreparedStatement使用dml语句
*/
public class PreparedStatement_pre_DML {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner scanner = new Scanner(System.in);
System.out.print("请输入学生姓名:");
String Sname=scanner.nextLine();
System.out.print("请输入学号:");
String Sno=scanner.nextLine();
//连接数据库
Properties properties =new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));//用getProperty()获取相关的值
String url = properties.getProperty("url");
String user= properties.getProperty("user");
String password=properties.getProperty("password");
String driver = properties.getProperty("driver");
//1.注册驱动
Class.forName(driver);
//2.获得连接
Connection connection = DriverManager.getConnection(url,user,password);//网络连接
//3.组织sql,SQL的?就相当于占位符等着后面设置值
String sql="delete from s where Sname=? AND Sno=?";
//4.获得Statement对象
PreparedStatement preparedStatement = connection.prepareStatement(sql);//preparedStatement对象 实现了PreparedStatement接口实现类的对象
preparedStatement.setString(1,Sname);//设置第一个问号那里的值
preparedStatement.setString(2,Sno);//第二个问号那里的值
//执行dml语句,使用
int rows = preparedStatement.executeUpdate();
System.out.println(rows > 0 ? "执行成功":"执行失败" );
//关闭
preparedStatement.close();
connection.close();
}
}
预处理DML和预处理查询很像但是他们用到了PreparedStatement接口中不同的方法:
1.查询用的是executeQuery()返回的ResultSet类型的
2.预处理DML用的是executeUpdate()返回int类型的rows即影响的行数
对于预处理DML的(insert,update,delete)只需要该上面代码的sql赋值就可以了