JDBC学习(三)---SQL注入问题/PreparedStatement对象

news/2024/5/19 0:21:14 标签: JDBC

JDBCSQLPreparedStatement_0">JDBC学习(三)—SQL注入问题/PreparedStatement对象

01 SQL注入问题

所谓SQL注入,就是通过把SQL命令插入到Web表单提交,最终达到欺骗服务器执行恶意的SQL命令。

程序示例:以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 users where name = '不存在的用户名' or 1=1;
            */
            System.out.print("请输入你要查询的用户名:");
            String username = scanner.nextLine();
            String sql = "select * from users where name =" +username;
			
            resultSet = statement.executeQuery(sql);

            while (resultSet.next()){
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getString("password"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //5.释放资源
            JDBCUtils.closeAll(resultSet,statement,connection);
        }

    }

}

运行结果:
在这里插入图片描述

02 PreparedStatement对象

PreperedStatement是Statement的子类,它的实例对象可以通过调用Connection.preparedStatement()方法获得

PreperedStatement可以解决SQL注入问题:PreparedStatement可对SQL进行预编译,从而提高数据库的执行效率。并且PreperedStatement对于sql中的参数,允许使用占位符的形式进行替换,简化sql语句的编写。

/*
preparedStatement的占位符:
preparedStatement.setXXX [xxx:对应占位符数据的类型] (?索引[从1开始] ,传入的值)
示例:
preparedStatement = connection.prepareStatement("select * from users where id = ?and name = ?");
preparedStatement.setInt(1,id);
preparedStatement.setString(1,username);
resultSet = preparedStatement.executeQuery();
*/

程序示例:

public class TestDemo2 {

    public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet resultSet = null;

        try {
            //1.获取数据库连接
            connection = JDBCUtils.getConnection();
			//2.输入SQL语句
            System.out.print("请输入你要查询的用户名:");
            String username = scanner.nextLine();
			//3.获取preparedStatement对象,预编译SQL语句
            preparedStatement = connection.prepareStatement("select * from users where name = ?");
            preparedStatement.setString(1,username);
            //4.执行SQL语句
            resultSet = preparedStatement.executeQuery();
			//5.获取结果
            while (resultSet.next()){
                System.out.println(resultSet.getString("name"));
                System.out.println(resultSet.getString("password"));
            }

        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            //6.释放资源
            JDBCUtils.closeAll(resultSet,preparedStatement,connection);
        }

    }
}

运行结果:不能通过SQL注入获取用户名和密码。
在这里插入图片描述


03 PreparedStatement和statement的区别

**相同点 : **

都是用来执行sql语句的

不同点 :
Statement 不安全 , 不能预编译SQL , 不能使用占位符 , 容易造成SQL语句拼接错误,不能防止SQL注入攻击
preparedStatement 安全 ,可以防止SQL注入攻击,可以预编译SQL语句 , 可以使用 ?作为占位符 。

Statement 先写SQL语句再执行;
​ preparedStatement 直接编译SQL , 调用方法执行 ; preparedStatement.execute();



http://www.niftyadmin.cn/n/1779859.html

相关文章

我的Java开发学习之旅------Workspace in use or cannot be created, choose a different one.--错误解决办法...

今天使用Eclipse时,突然卡死了,然后我强制关闭了Eclipse,再重新打开的时候就报错了,错误如下: Workspace in use or cannot be created, choose a different one. 错误原因:出现这种情况一般是workspace的…

阅读收藏夹

为什么80%的码农都做不了架构师?>>> 腾讯产品经理:BAT面试的时候喜欢怎样的新人(有好书) http://www.woshipm.com/pmd/284339.html 程序日志 http://macrochen.iteye.com/blog/1399082 转载于:https://my.oschina.net/body/blog/620134

MySQL学习(五)---MySQL备份数据库/三大范式

MySQL学习(五)—MySQL备份/数据库三大范式 01 MySQL备份 数据库备份的必要性:1.保证重要数据不丢失 2.数据转移MySQL数据库备份方法:1.mysqldump备份工具 2.数据库管理工具,如SQLyog 3.直接拷贝数据库文件和相关配置文件 mysqldump客户端的…

mysql-bin引起mysql不能启动

日志提示无法找到mysql-bin.000005这个文件 原因是我把其删除了,而在mysql-bin.index文件中却还有mysql-bin.000005的记录,因此启动失败了 解决: cd /var/lib/mysql vi mysql-bin.index 把有记录,但找不到文件的记录删除&#xf…

hdoj 1226 超级password 【隐图BFS】

称号:hdoj 1226 超级password 分析:这题属于隐式图搜索,状态不是非常明显,须要自己建立。 事实上搜索说白了就是暴力。 这个题目就是,首先对给出的能够组成的全部的数依次枚举。长度从小到大。 比方第一组例子&#x…

JDBC学习(四)---MySQL事务/MySQL的事务控制/JDBC操作事务

JDBC学习(四)—MySQL事务/MySQL的事务控制/JDBC操作事务 01 事务 MySQL事务:将一组SQL语句放在同一批次内去执行,如果一个SQL语句出错,则该批次内的所有SQL都将被取消执行。MySQL事务处理只支持InnoDB和BDB数据表类型。事务的原则&#xff…

分布式 Java 服务平台 Baratine

Baratine 是新的分布式,基于内存的 Java 服务平台,可以构建高性能的 Web 服务,在同一个 JVM 中结合数据和逻辑。在 Baratine 中,数据和服务是一体的,服务拥有它自己的数据: 数据不属于数据库 数据不能被其…

C#部署安装,将用户安装路径记录下写入注册表,并启动

安装部署程序,将安装目录写入注册表 (1)在“安装部署项目”上点击“注册表编辑器” (2)在HKey_LOCAL_MACHINE_SoftWare 下新建键 Manufacturer 代表软件的制造商。 (3) 右键新增字符串值,设置Name和Value属性。 Name NewPrjClient Value [TARGETDIR]N…