SQL注入
Statement详解
基本介绍
statement是一个接口(interface statement)
- Statement对象,用于执行静态SQL语句并返回其生成的结果的对象
- 在连接建立后,需要对数据库进行访问,执行命令或是SQL语句,可以通过
- Statemetn【存在SQL注入问题】
- PreparedStatement【预处理】
- CallableStatement【存储过程】
- Statement对象执行SQL语句,存在SQL注入风险
- SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命令,恶意攻击数据库
- 要防范SQL注入,只要用PreparedStatement(从Statement扩展而来)取代Statement就可以了
Navicat演示SQL注入
Create table admin (
Name varchar(32) not null unique,
pwd varchar(32) not null default ''
) character set utf8;
Insert into admin values('tom','123');
select * from admin where name = '1 ' or' and pwd = 'or '1' = '1';
JDBC演示SQL注入
package com.taotao.jdbc.statement;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class Statement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.print("请输入管理员姓名:");
String admin_name = sc.nextLine();
System.out.print("请输入管理员密码:");
String admin_pwd = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
Statement statement = connection.createStatement();
String sql = "select name,pwd from admin where name = '" + admin_name + "' and pwd = '"+ admin_pwd +"'";
ResultSet resultSet = statement.executeQuery(sql);
if (resultSet.next()){
System.out.println("恭喜,登陆成功");
}else {
System.out.println("登录失败");
}
resultSet.close();
statement.close();
connection.close();
}
}
PreparedStatement详解
基本介绍
- PreparedStatement执行的SQL语句中的参数用问号(?)来表示,调用PreparedStatement对象的setXxx()方法来设置这些参数.setXxx()方法有两个参数,第一个参数是要设置的SQL语句中的参数的索引(从1开始),第二个是设置的SQL语句中的参数的值
- 调用executeQuery(),返回ResultSet对象
- 调用executeUpdate(),执行更新,包括增、删、修改
预处理好处
- 不再使用 + 拼接sql语句,减少语法错误
- 有效的解决了sql注入问题!
- 大大减少了编译次数,效率较高
预处理案例(selete语句)
package com.taotao.jdbc.preparedstatement;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.print("请输入管理员姓名:");
String admin_name = sc.nextLine();
System.out.print("请输入管理员密码:");
String admin_pwd = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "select name,pwd from admin where name =? and pwd =? ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,admin_name);
preparedStatement.setString(2,admin_pwd);
ResultSet resultSet = preparedStatement.executeQuery();
if (resultSet.next()){
System.out.println("恭喜,登陆成功");
}else {
System.out.println("登录失败");
}
resultSet.close();
preparedStatement.close();
connection.close();
}
}
预处理案例(insert语句)
package com.taotao.jdbc.preparedstatement;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.print("请输入添加用户姓名");
String admin_name = sc.nextLine();
System.out.print("请输入添加用户密码");
String admin_pwd = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "insert into admin values(?,?) ";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,admin_name);
preparedStatement.setString(2,admin_pwd);
int i = preparedStatement.executeUpdate();
System.out.println("创建用户" + (i == 1 ? "成功":"失败" ));
preparedStatement.close();
connection.close();
}
}
预处理案例(update语句)
package com.taotao.jdbc.preparedstatement;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.print("请输入管理员姓名");
String admin_name = sc.nextLine();
System.out.print("请输入新的密码");
String admin_pwd = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "update admin set pwd = ? where name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(2,admin_name);
preparedStatement.setString(1,admin_pwd);
int i = preparedStatement.executeUpdate();
System.out.println("修改用户密码" + (i == 1 ? "成功":"失败" ));
preparedStatement.close();
connection.close();
}
}
预处理案例(delete语句)
package com.taotao.jdbc.preparedstatement;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要删除的管理员姓名");
String admin_name = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "delete from admin where name = ?";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
preparedStatement.setString(1,admin_name);
int i = preparedStatement.executeUpdate();
System.out.println("删除用户成功" + (i == 1 ? "成功":"失败" ));
preparedStatement.close();
connection.close();
}
}
预处理案例(create表)
package com.taotao.jdbc.preparedstatement;
import java.io.FileInputStream;
import java.io.IOException;
import java.sql.*;
import java.util.Properties;
import java.util.Scanner;
@SuppressWarnings({"all"})
public class PreparedStatement_ {
public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
Scanner sc = new Scanner(System.in);
System.out.print("请输入要创建的表名称");
String table_Name = sc.nextLine();
Properties properties = new Properties();
properties.load(new FileInputStream("src\\mysql.properties"));
String user = properties.getProperty("user");
String password = properties.getProperty("password");
String url = properties.getProperty("url");
Class.forName("com.mysql.cj.jdbc.Driver");
Connection connection = DriverManager.getConnection(url, user, password);
String sql = "create table "+table_Name+"(id int)";
PreparedStatement preparedStatement = connection.prepareStatement(sql);
boolean execute = preparedStatement.execute(sql);
System.out.println("创建表" + (execute == false ? "成功":"失败" ));
preparedStatement.close();
connection.close();
}
}
注意
JDBC的相关API小结
DriverManager驱动管理类
getConnection(url,user,pwd):获取连接
Connection接口
createStatement():生成命令对象
prepareStatement(sql):生成预编译命令对象
|