MySQL学习总结(6) — SQL注入问题 及 PreparedStatement
1. SQL注入问题
SQL注入,就是通过把SQL命令插入到Web表单提交,最终达到欺骗服务器执行恶意的SQL命令。
如上篇文章中对数据库的查询,通过输入用户名查询信息。
如果输入数据库表单中有的用户名,可以获得正确的结果
如果输入不存在的用户名,查询会失败
如果输入select * from users where name = '不存在的用户名' or 1=1;
public class TestDemo1 {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
try {
//1.获取数据库连接
connection = JDBCUtils.getConnection();
//2.创建statement对象
statement = connection.createStatement();
//3.编写SQL语句
/*
如果输入select * from use