jdbc-- StatementSQL注入演示

StatementSQL注入演示

解决方案:
preparedStatement
测试用户名: 1' or
测试密码: or '1'='1

// StatementSQL注入演示
    @Test
    public void statementSqlDemo() throws ClassNotFoundException, InstantiationException, IllegalAccessException, SQLException, IOException {

        // 注册驱动
        Class<?> aClass = Class.forName("com.mysql.jdbc.Driver");
        Driver driver =(Driver) aClass.newInstance();
        DriverManager.registerDriver(driver);
        FileInputStream fileInputStream = new FileInputStream("src/cn/usts/edu/config/db.properties");
        Properties properties = new Properties();
        properties.load(fileInputStream);
        String url=(String) properties.get("url");
        String user=(String) properties.get("user");
        String password=(String) properties.get("password");

        // 建立连接
        Connection connection = DriverManager.getConnection(url, user, password);

        // 执行sql
        Scanner scanner = new Scanner(System.in);
        System.out.println("输入用户名");//
        String name = scanner.nextLine();// nextLine不会空格切断    用户名 1' or
        System.out.println("输入密码");  
        String psd = scanner.nextLine();// nextLine不会空格切断   万能密码 or '1' = 1'
        String sql ="select admin.amin,admin.psd from admin where amin='"+name+"'and psd='"+psd+"'";

        Statement statement = connection.createStatement();
        ResultSet resultSet = statement.executeQuery(sql);

        // 获取结果
        if (resultSet.next()){ // 查询到结果才会有记录
            System.out.print("登陆成功");
        }else{
            System.out.print("登陆失败");
        }


        // 切断链接
        resultSet.close();
        statement.close();
        connection.close();


    }

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值