3.Statement对象概述,以及Statement的弊端

Statement对象

1. 概述

在java.sql包中有三个接口分别定义了对数据库的调用的不同方式:

  • Statement:用于执行静态sql语句并返回结果
  • PreparedStatement:sql语句被预编译并存储在此对象中,可以使用此对象多次高效的执行该sql语句;
  • CallableStatement:用于执行存储过程。

2. Statement和PreparedStatement 的区别

  1. PreparedStatement 是 Statement的子类;
  2. Statement 是sql拼串,有sql注入的问题;
  3. PreparedStatement ,使用sql预编译,随后再填充占位符,解决了sql注入的问题;
  4. PreparedStatement 可以操作Blob类型的数据;
  5. PreparedStatement 可以批量操作;

3. Statement的弊端(sql注入攻击)

接下来用一个例子演示,数据表如下

java 复制代码
public class StatementTest {

    @Test
    public void test() throws Exception {
        Connection connection = JDBCUtils.getConnection();

        Statement statement = connection.createStatement();

        String username = "AA";
        String password = "123456";
        String sql = "SELECT user,password,balance FROM user_table WHERE USER = '" + username + "' AND PASSWORD = '" + password + "'";

        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {

            String user = resultSet.getString(1);
            String pwd = resultSet.getString(2);

            User user1 = new User(user, pwd);
            System.out.println(user1);

        }

        JDBCUtils.close(connection,statement);
    }
}

可以成功查询到数据:

java 复制代码
User{user='AA', password='123456', balance=1000}

但是如果修改一下输入信息:

java 复制代码
String username = "1' or ";
String password = " ='1' or '1' = '1";
java 复制代码
public class StatementTest {

    @Test
    public void test() throws Exception {
        Connection connection = JDBCUtils.getConnection();

        Statement statement = connection.createStatement();

        String username = "1' or ";
        String password = " ='1' or '1' = '1";
        String sql = "SELECT user,password,balance FROM user_table WHERE USER = '" + username + "' AND PASSWORD = '" + password + "'";
        
        ResultSet resultSet = statement.executeQuery(sql);

        while (resultSet.next()) {

            String user = resultSet.getString(1);
            String pwd = resultSet.getString(2);
            int balance = resultSet.getInt(3);

            User user1 = new User(user, pwd, balance);
            System.out.println(user1);

        }

        JDBCUtils.close(connection,statement);
    }
}

输出结果:将所有数据都查出来了

java 复制代码
User{user='AA', password='123456', balance=1000}
User{user='BB', password='654321', balance=1000}
User{user='CC', password='abcd', balance=2000}
User{user='DD', password='abcder', balance=3000}

相当于执行了以下sql

sql 复制代码
SELECT user,password,balance FROM user_table WHERE USER = '1' or ' AND PASSWORD = ' ='1' or '1' = '1'
相关推荐
运维行者_10 小时前
企业无线网络监控的挑战与智能化演进趋势
大数据·运维·服务器·网络·数据库
国强_dev11 小时前
技术探讨:使用 stunnel 加密转发数据库连接时,如何获取客户端真实 IP?
数据库·网络协议·tcp/ip
@insist12311 小时前
系统规划与管理师-信息系统规划核心工作要点解析
数据库·软考·系统规划与管理师·软件水平考试·系统规划与管理工程师
超级数据查看器11 小时前
超级数据查看器 v10.0 发布
java·大数据·数据库·sqlite·安卓
数安3000天11 小时前
增量数据如何自动分类分级,避免目录“过期“?
大数据·数据库
南墙上的石头13 小时前
麒麟 V10 重装人大金仓 V8R6 踩坑实录(含 MySQL 兼容模式)
数据库·mysql
画中有画13 小时前
论向量数据库在项目中的应用
数据库
spider_xcxc14 小时前
Redis 数据库高质量实践指南(一)
运维·数据库·redis·oracle·云计算
l1t15 小时前
在linux和windows中解决duckdb 1.6dev版本输出执行计划报错问题
linux·运维·数据库·windows·duckdb
执子手 吹散苍茫茫烟波15 小时前
RC 隔离级别下 MySQL InnoDB 死锁典型案例
数据库·mysql