• ResultSet底层和Statement


    本次博客带领大家学习JDBC中的ResultSet底层和Statement。

    ResultSet的基本介绍

    • ResultSet表示数据库结果集的数据表,通常通过执行查询数据库的语句生成。

    • ResultSet对象保持一个光标指向其当前的数据行。 最初,光标位于第一行之前。 next方法将光标移动到下一行,并且由于在ResultSet对象中没有更多行时返回false ,因此可以在while循环中使用循环来遍历结果集。

    ResultSet的案例

    public class ResultSet_ {
        public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properites"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
    
            Statement statement = connection.createStatement();
            String sql = "select id,name,sex,borndate from actor";
            ResultSet resultSet = statement.executeQuery(sql);
            
            while (resultSet.next()){
                int id =resultSet.getInt(1);
                String name = resultSet.getString(2);
                String sex = resultSet.getString(3);
                Date date = resultSet.getDate(4);
                System.out.println(id+"\t"+name+"\t"+sex+"\t"+date);
            }
    
            resultSet.close();
            statement.close();
            connection.close();
        }
    }
    
    • 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

    Statement 的基本介绍

    1. Statement对象用于执行静态SQL语句并返回其生成的结果的对象。

    2. 在连接建立后,需要对数据库进行访问,执行命名或是SQL语句,可以通过

      • Statement [存在SQL注入]
      • PreparedStatement [预处理]
      • CallableStatement [存储过程]
    3. Statement 对象执行SQL语句,存在SQL注入风险。

    4. SQL注入是利用某些系统没有对用户输入的数据进行充分的检查,而在用户输入数据中注入非法的SQL语句段或命令,恶意攻击数据库。

    5. 要防范SQL注入,只要用PrepareStatement(从Statement扩展而来)取代Statement就可以了。

    Statement 的SQL注入案例

    public class Statement_ {
        public static void main(String[] args) throws IOException, ClassNotFoundException, SQLException {
            Scanner scanner = new Scanner(System.in);
            System.out.println("请输入管理员的名字:");
            String admin_name =scanner.nextLine();
            System.out.println("请输入管理员的密码");
            String admin_pwd=scanner.nextLine();
            Properties properties = new Properties();
            properties.load(new FileInputStream("src\\mysql.properites"));
            String user = properties.getProperty("user");
            String password = properties.getProperty("password");
            String driver = properties.getProperty("driver");
            String url = properties.getProperty("url");
            Class.forName(driver);
            Connection connection = DriverManager.getConnection(url, user, password);
    
            Statement statement = connection.createStatement();
            String sql = "select name,pwd from admin where name ='"+admin_name+"' and pwd ='"+admin_pwd+"'";
            ResultSet resultSet = statement.executeQuery(sql);
            if(resultSet.next()){
                System.out.println("恭喜登录成功!");
            }else{
                System.out.println("对不起,登陆失败");
            }
            resultSet.close();
            statement.close();
            connection.close();
        }
    }
    
    • 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
    CREATE TABLE admin(
    	NAME VARCHAR(32) NOT NULL UNIQUE,
    	pwd VARCHAR(32) NOT NULL DEFAULT '') CHARACTER SET utf8;
    	
    INSERT INTO admin VALUES('tom','123');
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 输入数据库不存在的数据也能登录成功!
      请添加图片描述
  • 相关阅读:
    ElasticSearch从入门到精通--第三话(集群环境搭建篇)
    OpenCV之九宫格图像
    设计模式-创建型模式-建造者模式
    分布式项目学习笔记(二):本地服务发布成RPC服务
    C#导出本机Win32native dll
    Matlab绘图(1)通过属性检查器调整绘图
    【计算机网络】网络层和数据链路层
    FPGA实现AD采集
    Python少儿编程提高篇(4)集合
    DockerFile打包项目实战解析,一文读懂dockerfile打包
  • 原文地址:https://blog.csdn.net/lidong777777/article/details/126732699