zoukankan      html  css  js  c++  java
  • PreparedStatement

    jdbc六部曲:1.导包 2.加载驱动 3.创建连接 4.创建状态参数 5.执行sql 6.关闭

            String className="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/atm?useSSL=false";
            String user="hh";
            String pwd="123456";
            String sql="select * from balance where username='"+username+"'";
    
            Connection conn=null;
            Statement stat=null;
            ResultSet rs=null;
            try {
                Class.forName(className);//2
                conn= DriverManager.getConnection(url,user,pwd);//3
                stat=conn.createStatement();//4
                rs=stat.executeQuery(sql);//5
    }catch (Exception e) {
                e.printStackTrace();
            }finally {//6
                try {
                    if (rs != null) {
                        rs.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    if(stat!=null){
                        stat.close();
                    }
    
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    if(conn!=null){
                        conn.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }

    上面代码状态参数是Statement,以后不建议用它了 ,原因如下

    跟字符串拼接有关
    1.SQL注入
      所谓的SQL注入
      意思就是通过SQL命令 拼接其他的字符串
      让其他的那些字符串来改变原有SQL语句的执行
      最终达到欺骗服务器的效果
      里面拼接的其他字符 肯定是SQL语法认可的合法的
      select * from atm where aname = 'xxx' and apassword = 'xxx' 【or '1' = '1'】

    2.问题产生的原因
      1.判断不严谨导致的
      2.SQL语句问题 允许拼接字符串 认为用户很不安全
      3.可以利用PreparedStatement来处理SQL

    Statement                     PreparedStatement
    普通的状态参数            预处理状态参数
    创建时不需要SQL         创建时就需要预先加载SQL语句
    此时没有执行               此时没有执行 但 底层预先处理SQL需要查询的结果 性能高
                可以利用动态化进行参数的处理 利用?代替 数据类型及值
    PreparedStatement好处 1.增强SQL可读性 2.可以参数动态化 3.防止SQL注入 4.提高执行性能

     String className="com.mysql.jdbc.Driver";
            String url="jdbc:mysql://localhost:3306/atm?useSSL=false";
            String user="hh";
            String pwd="123456";
            String sql="select * from balance where username= ?";
    
            Connection conn=null;
            PreparedStatement pstat=null;
            ResultSet rs=null;
            try {
                Class.forName(className);
                conn= DriverManager.getConnection(url,user,pwd);
                pstat=conn.prepareStatement(sql);//sql语句预处理
           
    pstat.setString(1,username);//1代表sql语句中第1个?,以此类推
                rs=pstat.executeQuery();//这里就不传sql了
                if(rs.next()){
                 
                }
            } catch (Exception e) {
                e.printStackTrace();
            }finally {
                try {
                    if (rs != null) {
                        rs.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    if(pstat!=null){
                        pstat.close();
                    }
    
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
                try {
                    if(conn!=null){
                        conn.close();
                    }
                } catch (SQLException throwables) {
                    throwables.printStackTrace();
                }
            }
  • 相关阅读:
    Premetheus告警QQ邮箱
    Prometheus+grafana监控SpringBoot2应用
    Grafana整合Prometheus
    Prometheus:入门初体验
    接口幂等性思路
    OpenFeign远程调用丢失请求头问题解决办法
    gradle构建脚本
    windows安装gradle
    CompletableFuture异步编排
    线程池(ThreadPoolExcutor)基本介绍
  • 原文地址:https://www.cnblogs.com/hebiao/p/14516471.html
Copyright © 2011-2022 走看看