posts - 397,comments - 0,views - 25332

SQL注入问题∶在拼接sql时,有一些sql的特殊关键字参与字符串的拼接。会造成安全性问题

1.输入用户随便,输入密码:a' or 'a' = 'a

2. sql : select * from user where username = 'fhdsjkf' and password = 'a' or 'a' = 'a'

解决sql注入问题:使用Preparedstatement对象来解决

预编译的sQL:参数使用?作为占位符

  步骤:

  导入驱动jar包mysql-connector-java-5.1.37-bin.jar注册驱动

  获取数据库连接对象connection定义sql

  注意: sql的参数使用?作为占位符。如: select * from user where username = ? and password = ?;获取执行sql语句的对象preparedstatement connection.preparestatement(string sql)
  给﹖赋值:
  方法: setXxx(参数1,参数2)

  参数1:?的位置编号从1开始*参数2:?的值

  执行sql,接受返回结果,不需要传递sql语句处理结果

  释放资源

 

java代码:

复制代码
public static boolean login1(String username,String password){
        if (username == null || password == null){
            return false;
        }
        Connection conn = null;
        PreparedStatement statement = null;
        ResultSet rs = null;
        try {
            conn = JDBCUtils.getConnection();
            String sql = "select * from user where username = ? and password = ?";
            statement = conn.prepareStatement(sql);
            statement.setString(1,username);
            statement.setString(2,password);
            rs = statement.executeQuery();
            return rs.next();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return false;

    }
复制代码

 

 

 

 

 

 

 

 

 

 

 

 

Jdbc事务的管理_概述

  事务:一个包含多个步骤的业务操作,如果这个业务操作被事务管理,则多个步骤同时成功要么同时失败

    操作

      开启事务

      提交事务

      回滚事务

    使用Connection对象来管理事务对象

      开启事务:setAutoCommit(boolaen autoCommit) 使用该方法设置参数false,级开启事务

      提交事务:commit

      回滚事务:rollback

 

posted on   淤泥不染  阅读(27)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5

点击右上角即可分享
微信分享提示