Day 73

_JDBC-API详解-ResultSet

  1. ResultSet(结果集对象)作用:

    • 封装了DQL查询语句的结果

      • ResultSet   stmt.executeQuery(sql):执行DQL语句,返回ResultSet对象
        
    • 获取查询结果

      • boolean next():1.将光标从当前位置向前移动一行;2.判断当前行是否有效
            返回值:
            	- true:有效行,当前行有数据
            	- false:无效行,当前行没有数据
        
      • xxx getXxxx(参数):获取数据
            xxx:数据类型; int getInt(参数); String getString(参数)
                参数:
                - int :列的编号
                - String :列的名称
        
  2. package com.jdbcstudy;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class JDBC_Demo_03 {
        public static void main(String[] args) {
            /*
            DQL查询语句
             */
    
            // 创建驱动连接
            String url = "jdbc:mysql://127.0.0.1:3306/atguigudb";
            String username = "root";
            String password = "abc123";
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url,username,password);
    
                // 定义sql语句
                String sql_01 = "select * from student";
    
                // 创建执行sql的对象
                Statement stmt = conn.createStatement();
    
                // 执行sql语句
                ResultSet res = stmt.executeQuery(sql_01);
    
                // 创建返回结果List集合
                List<Account> list = new ArrayList<>();
    
                // 处理返回的结果
                while (res.next()) {
                    int id = res.getInt(1);
                    String name = res.getString(2);
                    int age = res.getInt(3);
                    int class_ = res.getInt(4);
    
                    //将获取到的数据打包
                    Account account = new Account();
    
                    account.setId(id);
                    account.setName(name);
                    account.setAge(age);
                    account.setClass_(class_);
    
                    list.add(account);
                }
                System.out.println(list);
    
                // 释放资源
                res.close();
                stmt.close();
                conn.close();
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    =================================
    [Account{id=1, name='李华', age=43, class_=1}, Account{id=2, name='小明', age=123, class_=1}, Account{id=3, name='小红', age=27, class_=2}, Account{id=4, name='小金', age=29, class_=2}]
    
    Process finished with exit code 0
    

_JDBC-API详解-PreparedStatement-SQL注入

  1. PreparedStatement作用:
    • 预编译SQL语句进行执行,防止sSQL注入问题
  2. SQL注入:SQL注入时通过操作输入来修改实现定义好的SQL语句,用以达到执行代码对服务器进行攻击的方法

_JDBC-API详解-PreparedStatement

  1. PreparedStatement作用:

    • 预编译SQL并执行SQL语句

      • 获取PreparedStatement对象

      • //SQL语句中的参数值,使用?占位符替代
        String sql = "select * from user where username = ? and password = ?";
        // 通过Connection对象获取,并传入对应的sql语句
        PreparedStatement pstmt = conn.PreparedStatement(sql);
        
      • 参数设置

      • PreparedStatement 对象: setXxx(参数1,参数2):给?赋值
            Xxx:数据类型;setInt(参数1,参数2)
            参数:
                参数1? 的位置编号,从**1**开始
                参数2? 的值
        
      • 执行sql

      • executeUptate(); 
        executeQuery();
        不需要传递sql
        
  2. package com.jdbcstudy;
    
    import java.sql.*;
    import java.util.ArrayList;
    import java.util.List;
    
    public class JDBC_Demo_04 {
        public static void main(String[] args) {
            /*
            //SQL语句中的参数值,使用?占位符替代
            String sql = "select * from user where username = ? and password = ?";
            // 通过Connection对象获取,并传入对应的sql语句
            PreparedStatement pstmt = conn.PreparedStatement(sql);
    
            PreparedStatement 对象: setXxx(参数1,参数2):给?赋值
            Xxx:数据类型; 如setInt(参数1,参数2)
            参数:
                参数1:? 的位置编号,从**1**开始
                参数2:? 的值
             */
    
            // 创建驱动连接
            String url = "jdbc:mysql://127.0.0.1:3306/atguigudb";
            String username = "root";
            String passw = "abc123";
            Connection conn = null;
            try {
                conn = DriverManager.getConnection(url, username, passw);
    
                // 接收用户输入的用户名和密码
                String name = "小明";
                String psw = "1234567";
    
    
                // 定义sql语句
                String sql_01 = "select * from student where name =? and password =?";
    
    
                // 通过Connection对象获取,并传入对应的sql语句
                PreparedStatement pst = conn.prepareStatement(sql_01);
    
                pst.setString(1,name);
                pst.setString(2, psw);
    
                // 执行sql语句
                ResultSet res = pst.executeQuery();
    
                // 处理sql的返回结果
                List<Account> list = new ArrayList<>();
    
                while (res.next()) {
                    // 将返回的结果创建对象打包
                    Account account = new Account();
                    int id = res.getInt(1);
                    String name_ = res.getString(2);
                    int age = res.getInt(3);
                    int class_ = res.getInt(4);
                    String password = res.getString(5);
    
                    account.setId(id);
                    account.setName(name_);
                    account.setAge(age);
                    account.setClass_(class_);
                    account.setPassword(password);
    
                    list.add(account);
                }
                System.out.println(list);
    
                // 释放资源
                res.close();
                pst.close();
                conn.close();
    
            } catch (SQLException e) {
                e.printStackTrace();
            }
        }
    }
    =================================
    [Account{id=2, name='小明', age=123, class_=1, password='1234567'}]
    
    Process finished with exit code 0
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值