解决SQL注入问题

什么是SQL注入

        当我们使用Statement进行操作时,在执行sql语句时,存在参数拼凑成恒等表达式的问题,如以下代码

public class TestStatement {
    public static void main(String[] args) {
        String url = "jdbc:oracle:thin:@192.168.200.128:1521:XE";
        String username = "HR";
        String password = "123456";

        Connection con = null;
        Statement stm = null;
        ResultSet rs = null;
        Scanner scanner = new Scanner(System.in);

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(url, username, password);
            System.out.println("连接成功!");

            // 3.查询所有年级数据
            System.out.println("学生数据库系统登录界面");

            System.out.print("请输入学号:");
            int studentNo = Integer.parseInt(scanner.nextLine());
            System.out.print("请输入密码:");
            String pwd = scanner.nextLine();

            // 3.1 准备sql语句
            String sql = "select count(*) from student where studentno=" + studentNo +
                    " and loginpwd='" + pwd + "'";
            // 3.2 创建statement对象
            stm = con.createStatement();
            // 3.3 执行sql语句
            rs = stm.executeQuery(sql);
            // 3.4 解析返回数据
            int result = -1;
            if (rs.next()) {
                result = rs.getInt(1);
            }
            // 判断结果
            if (result > 0) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败,学号或密码有误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 4.释放资源
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("断开连接!");
        }
    }
}

        当我们输入

' or 1=1 --'

        时,无论如何都能登陆数据库(产生了恒等式,--'' 变为了注释)

select count(*) from student where studentno=1000 and loginpwd='' or 1=1 

         上述情况,我们把它成为SQL注入问题

PreparedStatement接口

  • 继承自 Statement接口

  • 执行的是预编译的 SQL 语句

  • 比Statement对象使用起来更加灵活,更有效率

  • 提高了代码的可读性和可维护性

  • 提高了安全性

        使用步骤如下:

1.声明PreparedStatement变量
2.使用带占位符的sql语句
3.创建PreparedStatement对象
4.设置每一个输入参数的值
5.执行sql语句
6.关闭PreparedStatement

        优化代码如下:

public class TestPreparedStatement {
    public static void main(String[] args) {
        String url = "jdbc:oracle:thin:@192.168.200.128:1521:XE";
        String username = "HR";
        String password = "123456";

        Connection con = null;
        PreparedStatement stm = null;
        ResultSet rs = null;
        Scanner scanner = new Scanner(System.in);

        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            con = DriverManager.getConnection(url, username, password);
            System.out.println("连接成功!");

            // 3.查询所有年级数据
            System.out.println("学生数据库系统登录界面");
            System.out.print("请输入学号:");
            int studentNo = Integer.parseInt(scanner.nextLine());
            System.out.print("请输入密码:");
            String pwd = scanner.nextLine();

            // 3.1 准备sql语句, sql语句中所有的值,都换成?进行占位
            String sql = "select count(*) from student where studentno=? and loginpwd=?";
            System.out.println(sql);
            // 3.2 创建statement对象
            stm = con.prepareStatement(sql);
            // *****为sql中的占位符赋值****
            stm.setInt(1, studentNo);
            stm.setString(2,pwd);

            // 3.3 执行sql语句
            rs = stm.executeQuery();
            // 3.4 解析返回数据
            int result = -1;
            if (rs.next()) {
                result = rs.getInt(1);
            }
            // 判断结果
            if (result > 0) {
                System.out.println("登录成功");
            } else {
                System.out.println("登录失败,学号或密码有误!");
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                // 4.释放资源
                if (rs != null) {
                    rs.close();
                }
                if (stm != null) {
                    stm.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println("断开连接!");
        }
    }
}

        这样,我们就避免了SQL注入问题

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值